0

在 R 中执行 k-means 后是否可以获得每个集群内的实际观察值?

例如,在我的分析之后,我有 2 个集群,我想在每个集群中找到确切的观察结果,这可能吗?

4

1 回答 1

4
# random samples
x <- matrix(c(rnorm(30,10,2), rnorm(30,0,1)), nrow=12, byrow=T)

# clustering
clusters <-  kmeans(x, 2)

# accessing cluster membership 
clusters$cluster
[1] 1 1 1 1 1 1 2 2 2 2 2 2

# samples within cluster 1
c1 <- x[which(clusters$cluster == 1),]


# samples within cluster 2
c2 <- x[which(clusters$cluster == 2),]


# printing variables
x
           [,1]       [,2]         [,3]       [,4]       [,5]
 [1,] 10.8415151  9.3075438  9.443433171 13.5402818  7.0574904
 [2,]  6.0721775  7.4570368  9.999411972 12.8186182  6.1697638
 [3,] 11.3170525 10.9458832  7.576416396 12.7177707  6.7104535
 [4,]  8.1377999  8.0558304  9.925363089 11.6547736  9.4911071
 [5,] 11.6078294  8.7782984  8.619840508 12.2816048  9.4460169
 [6,] 10.2972477  9.1498916 11.769122361  7.6224395 12.0658246
 [7,] -0.9373027 -0.5051318 -0.530429758 -0.8200562 -0.0623147
 [8,] -0.7257655 -1.1469400 -0.297539831 -0.0477345 -1.0278240
 [9,]  0.7285393 -0.6621878  2.914976054  0.6390049 -0.5032553
[10,]  0.2672737 -0.6393167 -0.198287317  0.1430110 -2.2213365
[11,] -0.8679649  0.3354149 -0.003510304  0.6665495  0.6664689
[12,]  0.1731384 -1.8827645  0.270357961  0.3944154  1.3564678

c1
          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 10.841515  9.307544  9.443433 13.540282  7.057490
[2,]  6.072177  7.457037  9.999412 12.818618  6.169764
[3,] 11.317053 10.945883  7.576416 12.717771  6.710454
[4,]  8.137800  8.055830  9.925363 11.654774  9.491107
[5,] 11.607829  8.778298  8.619841 12.281605  9.446017
[6,] 10.297248  9.149892 11.769122  7.622439 12.065825

c2
           [,1]       [,2]         [,3]       [,4]       [,5]
[1,] -0.9373027 -0.5051318 -0.530429758 -0.8200562 -0.0623147
[2,] -0.7257655 -1.1469400 -0.297539831 -0.0477345 -1.0278240
[3,]  0.7285393 -0.6621878  2.914976054  0.6390049 -0.5032553
[4,]  0.2672737 -0.6393167 -0.198287317  0.1430110 -2.2213365
[5,] -0.8679649  0.3354149 -0.003510304  0.6665495  0.6664689
[6,]  0.1731384 -1.8827645  0.270357961  0.3944154  1.3564678
于 2013-03-15T21:37:43.767 回答