很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
9年前关闭。
我通过 k-means 聚类方法对数据进行聚类,如何使用 R 中的 k-means 聚类技术获得与数据相对应的聚类数?为了得到每条记录属于哪个簇。
例子12 32 13 => 1. 12,13 2. 32
最佳答案
听起来您正在尝试访问 kmeans()
返回的集群向量.从集群的帮助页面:
A vector of integers (from 1:k) indicating the cluster to which each
point is allocated.
使用帮助页面上的示例:
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
#Access the cluster vector
cl$cluster
> cl$cluster
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[45] 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[89] 1 1 1 1 1 1 1 1 1 1 1 1
解决评论中的问题
您可以通过执行以下操作将集群编号“映射”到原始数据:
out <- cbind(x, clusterNum = cl$cluster)
head(out)
x y clusterNum
[1,] -0.42480483 -0.2168085 2
[2,] -0.06272004 0.3641157 2
[3,] 0.08207316 0.2215622 2
[4,] -0.19539844 0.1306106 2
[5,] -0.26429056 -0.3249288 2
[6,] 0.09096253 -0.2158603 2
cbind
是列绑定(bind)的函数,还有一个rbind
行的函数。有关详细信息,请参阅他们的帮助页面 ?cbind
和 ?rbind
分别。
关于r - 如何使用 R 中的 k-means 聚类技术获得与数据相对应的聚类编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8278966/