2013-02-22 76 views
-4

在R,I已經計算的k均值聚類,如下所示:- [R k均值聚類數據

km = (mat2, centers=3) 

其中MAT2是通過組合一組時間序列的的元件獲得的列向量的矩陣。有31行

既然我有我的k-means對象,我該如何看待與特定點相關的數據?例如,假設我點擊屬於其中一個分區的點。我如何查看這些數據?當然我的意思是如何以編程方式獲取這些數據。

+2

您需要添加更多的細節;不可能遵循你正在做的事情。 – csgillespie 2013-02-22 08:40:27

回答

2

我希望你叫kmeans就象這樣:

set.seed(42) 
df <- data.frame(row.names = paste0("obs", 1:100), 
         V1 = rnorm(100), 
         V2 = rnorm(100), 
         V3 = rnorm(100)) 
km <- kmeans(df, centers = 3) 

如果您不熟悉的一項新功能,它總是一個好主意,用str()檢查結果對象:

> str(km) 
List of 7 
$ cluster  : Named int [1:100] 1 2 3 3 1 1 1 1 1 1 ... 
    ..- attr(*, "names")= chr [1:100] "obs1" "obs2" "obs3" "obs4" ... 
$ centers  : num [1:3, 1:3] 0.65604 -1.09689 0.56428 0.11162 0.00549 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:3] "1" "2" "3" 
    .. ..$ : chr [1:3] "V1" "V2" "V3" 
$ totss  : num 291 
$ withinss : num [1:3] 43.7 65.7 51.3 
$ tot.withinss: num 161 
$ betweenss : num 130 
$ size  : int [1:3] 36 34 30 
- attr(*, "class")= chr "kmeans" 

由於我從你的問題中瞭解到,你正在尋找km$cluster,它告訴你哪些數據觀察已分配給哪個集羣。集羣中心因此可以通過km$centers進行調查。

如果你現在想知道哪些意見已聚集與中心km$centers[3,]第三集羣,您可以子集的data.frame(或matrix)由

> rownames(df[ km$cluster == 3, ]) 
[1] "obs3" "obs4" "obs12" "obs15" "obs16" "obs21" "obs25" "obs27" "obs32" "obs42" "obs43" "obs46" "obs48" "obs54" "obs55" "obs58" "obs61" "obs62" "obs63" "obs66" "obs67" "obs73" "obs76" 
[24] "obs77" "obs81" "obs84" "obs86" "obs87" "obs90" "obs94"