简体   繁体   中英

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

I believe the more direct way to do it is to use cutree (and not go through rect.dendrogram):

> 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.

You need to access the data stored in your rect.hclust object, along these lines:

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. You can export these into a tabular form, for example by using lapply :

labels <- lapply(obj, paste0, collapse = ",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM