データセット ( data.matrixという名前のデータフレーム) に対してクラスター分析を実行した後、各インスタンスが属するクラスター名を含む、クラスターという名前の新しい列を末尾 (列 27) に追加しました。
私が今欲しいのは、各クラスターの代表的なインスタンスです。クラスターの重心から最小のユークリッド距離を持つインスタンスを見つけようとしました (そして、クラスターごとに手順を繰り返します)。
これが私がしたことです。他の - おそらくもっとエレガントな - 方法を考えられますか? (null のない数値列を想定します)。
clusters <- levels(data.matrix$cluster)
cluster_col = c(27)
for (j in 1:length(clusters)) {
# get the subset for cluster j
data = data.matrix[data.matrix$cluster == clusters[j],]
# remove the cluster column
data <- data[,-cluster_col]
# calculate the centroid
cent <- mean(data)
# copy data to data.matrix_cl, attaching a distance column at the end
data.matrix_cl <- cbind(data, dist = apply(data, 1, function(x) {sqrt(sum((x - cent)^2))}))
# get instances with min distance
candidates <- data.matrix_cl[data.matrix_cl$dist == min(data.matrix_cl$dist),]
# print their rownames
print(paste("Candidates for cluster ",j))
print(rownames(candidates))
}