From this dendrogram, I would like to extract the height for each leaf. In this case example, I would like to get back a list like this :
list("A" = 2.07, "B" = 5.09, "C" = 3.12, "D" = 2.07, "E" = 3.12)
Here's the code for this example:
m <- matrix(rnorm(30), nrow = 5)
rownames(m) <- LETTERS[1:5]
d <- dist(m)
h <- hclust(d)
plot(h)
I can extract the height from the dendrogram easily with
h$height
And see the information I want with
str(as.dendrogram(h))
But I couldn't find a way to extract what I want. I also looked into the dendextend package but I couldn't find what I want.
Do you know a function that already does this? I'm not very familiar with S3 objects...
You could use the
dendextendpackage to first convert it to aas.dendrogramobject. After that you can extract the labels with thelabelsfunction. To get the heights of the labels you first convert it withhang.dendrogramand then extract the height attribute of the leaves usingget_leaves_attrlike this:If you want to have it in a list you could
setNameswithas.listlike this:Created on 2023-03-30 with reprex v2.0.2