[英]How can we put label of hclust in table according to clusters formed in dendogram
I have a dendogram with clusters having many leaf nodes/labels How can I put these labels of hclust in a table row wise according to the clusters formed using R我有一个具有许多叶节点/标签的集群的树状图如何根据使用 R 形成的集群将 hclust 的这些标签逐行放在表中
I believe the more direct way to do it is to use cutree
(and not go through rect.dendrogram):我相信更直接的方法是使用
cutree
(而不是通过 rect.dendrogram 使用 go):
> d1 <- USArrests[1:10,]
> d1 <- USArrests[1:10,]
> hc <- hclust(dist(d1))
> hcc <- cutree(hc, k = 3)
> hcc
Alabama Alaska Arizona Arkansas California
1 1 2 1 2
Colorado Connecticut Delaware Florida Georgia
1 3 1 2 1
> data.frame(d1, hcc)
Murder Assault UrbanPop Rape hcc
Alabama 13.2 236 58 21.2 1
Alaska 10.0 263 48 44.5 1
Arizona 8.1 294 80 31.0 2
Arkansas 8.8 190 50 19.5 1
California 9.0 276 91 40.6 2
Colorado 7.9 204 78 38.7 1
Connecticut 3.3 110 77 11.1 3
Delaware 5.9 238 72 15.8 1
Florida 15.4 335 80 31.9 2
Georgia 17.4 211 60 25.8 1
pro tip: if you're using dendrogram instead of hclut, you can use the dendextend package in order to have cutree for dendrogram as well.专业提示:如果您使用 dendrogram 而不是 hclut,则可以使用dendextend package 以获得树状图的cutree。
You need to access the data stored in your rect.hclust
object, along these lines:您需要访问存储在
rect.hclust
object 中的数据,如下所示:
obj <- rect.hclust(my_matrix, k = n)
str(obj)
By inspecting obj
with str
you will find a list of all the variables grouped by cluster.通过使用
str
检查obj
,您将找到按集群分组的所有变量的列表。 You can export these into a tabular form, for example by using lapply
:您可以将它们导出为表格形式,例如使用
lapply
:
labels <- lapply(obj, paste0, collapse = ",")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.