一旦你使用 , 建立集群hclust
,然后cutree
指定下集群;你如何检索形成不同集群的成员?假设您创建了一个简单的层次集群,什么命令可以检索在集群组的叶子中“单独”计算的元素?
我试过了,table()
但没有办法...
一旦你使用 , 建立集群hclust
,然后cutree
指定下集群;你如何检索形成不同集群的成员?假设您创建了一个简单的层次集群,什么命令可以检索在集群组的叶子中“单独”计算的元素?
我试过了,table()
但没有办法...
您可以使用该split
函数:对于定义数据应如何分组的第二个参数,使用您的树切割。它将返回一个列表,其中每个元素都是不同的集群。
hc <- hclust(dist(USArrests), "ave")
ct <- cutree(hc, k=3)
如果您只想要成员名称:
split(names(ct), ct)
# $`1`
# [1] "Alabama" "Alaska" "Arizona" "California"
# [5] "Delaware" "Florida" "Illinois" "Louisiana"
# [9] "Maryland" "Michigan" "Mississippi" "Nevada"
# [13] "New Mexico" "New York" "North Carolina" "South Carolina"
# $`2`
# [1] "Arkansas" "Colorado" "Georgia" "Massachusetts"
# [5] "Missouri" "New Jersey" "Oklahoma" "Oregon"
# [9] "Rhode Island" "Tennessee" "Texas" "Virginia"
# [13] "Washington" "Wyoming"
# $`3`
# [1] "Connecticut" "Hawaii" "Idaho" "Indiana"
# [5] "Iowa" "Kansas" "Kentucky" "Maine"
# [9] "Minnesota" "Montana" "Nebraska" "New Hampshire"
# [13] "North Dakota" "Ohio" "Pennsylvania" "South Dakota"
# [17] "Utah" "Vermont" "West Virginia" "Wisconsin"
或者如果您希望原始数据按集群拆分:
split(USArrests, ct)
# $`1`
# Murder Assault UrbanPop Rape
# Alabama 13.2 236 58 21.2
# Alaska 10.0 263 48 44.5
# Arizona 8.1 294 80 31.0
# [...]
# $`2`
# Murder Assault UrbanPop Rape
# Arkansas 8.8 190 50 19.5
# Colorado 7.9 204 78 38.7
# Georgia 17.4 211 60 25.8
# [...]
# $`3`
# Murder Assault UrbanPop Rape
# Connecticut 3.3 110 77 11.1
# Hawaii 5.3 46 83 20.2
# Idaho 2.6 120 54 14.2
# [...]