简体   繁体   中英

How to get the new hclust object after using cutree function in R

How do I get the new hclust object after using cutree function? I know the standard output from cutree is a numerical vector that tells you the clusters each leaf node belongs. However, how can we get the aggregated tree structure after cutting at a certain height or by a given number of clusters?

library(gplots)
library(dendextend)

data <- iris[,1:4]
distance <- dist(data, method = "euclidean", diag = FALSE, upper = FALSE)
hc <- hclust(distance, method = 'ward.D')
dnd <- as.dendrogram(hc)
plot(dnd) 
abline(h=hc$height[140],col="red")

I searched related topics, but unfortunately cant find a solution.

Try this:

library(dplyr)
numberGroup = 10
dataClust = cbind(data, clust = cutree(hc, numberGroup))
dataClust = dataClust %>%
  group_by(clust) %>%
  summarise_all(mean)
dataClust %>%
  dist %>%
  hclust("ward.D2") %>%
  plot

You can use cut to get the pieces of the dendrogram. Since you seem to be interested in the height h=hc$height[140]

CutDend = cut(dnd, h=hc$height[140])
plot(CutDend$upper)

树状图的顶部

BTW, you can also get the 10 branches below the cut line using CutDend$lower

Some additional details are available via help(cut.dendrogram)

cut.dendrogram() returns a list with components $upper and $lower, the first is a truncated version of the original tree, also of class dendrogram, the latter a list with the branches obtained from cutting the tree, each a dendrogram.

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