Search code examples
rattributesdendrogramdendextend

How to access attributes of a dendrogram in R


From a dendrogram which i created with

hc<-hclust(kk)
hcd<-as.dendrogram(hc)

i picked a subbranch

k=hcd[[2]][[2]][[2]][[2]][[2]][[2]][[2]][1]

When i simply have k displayed, this gives:

> k
[[1]]
[[1]][[1]]
[1] 243
attr(,"label")
[1] "NAfrica_002"
attr(,"members")
[1] 1
attr(,"height")
[1] 0
attr(,"leaf")
[1] TRUE

[[1]][[2]]
[1] 257
attr(,"label")
[1] "NAfrica_016"
attr(,"members")
[1] 1
attr(,"height")
[1] 0
attr(,"leaf")
[1] TRUE

attr(,"members")
[1] 2
attr(,"midpoint")
[1] 0.5
attr(,"height")
[1] 37

How can i access, for example, the "midpoint" attribute, or the second of the "label" attributes?

(I hope i use the correct terminology here)

I have tried things like

k$midpoint
attr(k,"midpoint")

but both returned 'NULL'.

Sorry for question number 2: how could i add a "label" attribute after the attribute "midpoint"?


Solution

  • Your k is still buried one layer too deep. The attributes have been set on the first element of the list k.

     attributes(k[[1]]) # Display attributes
     attributes(k[[1]])$label # Access attributes
     attributes(k[[1]])$label <- 'new' # Change attribute
    

    Alternatively, you can use attr:

    attr(k[[1]],'label') # Display attribute