3

enter image description here

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

Quinten
  • 35,235
  • 5
  • 20
  • 53

1 Answers1

2

You could use the dendextend package to first convert it to a as.dendrogram object. After that you can extract the labels with the labels function. To get the heights of the labels you first convert it with hang.dendrogram and then extract the height attribute of the leaves using get_leaves_attr like this:

library(dplyr)
library(dendextend)

set.seed(7) # reproducibility
m <- matrix(rnorm(30), nrow = 5)
rownames(m) <- LETTERS[1:5]
d <- dist(m)
h <- hclust(d)
plot(h)

dend <- as.dendrogram(h)
labels = dend %>% labels
heights = dend %>% hang.dendrogram %>% get_leaves_attr("height")
data.frame(labels = labels,
           heights = heights)
#>   labels   heights
#> 1      E 2.3400492
#> 2      B 0.9012174
#> 3      C 0.9012174
#> 4      A 3.3943293
#> 5      D 3.3943293

If you want to have it in a list you could setNames with as.list like this:

as.list(setNames(heights, labels))
#> $E
#> [1] 2.340049
#> 
#> $B
#> [1] 0.9012174
#> 
#> $C
#> [1] 0.9012174
#> 
#> $A
#> [1] 3.394329
#> 
#> $D
#> [1] 3.394329

Created on 2023-03-30 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53