Get/set attributes of dendrogram's leaves

get_leaves_attr(dend, attribute, simplify = TRUE, ...)

Source

Heavily inspired by the code in the function labels.dendrogram, so credit should go to Martin Maechler.

Arguments

dend

a dendrogram object

attribute

character scalar of the attribute (attr) we wish to get/set from the leaves

simplify

logical. If TRUE (default), then the return vector is after using unlist on it.

...

not used

Value

A vector (or a list) with the dendrogram's leaves attribute

Examples

# define dendrogram object to play with:
hc <- hclust(dist(USArrests[1:3, ]), "ave")
dend <- as.dendrogram(hc)

# get_leaves_attr(dend) # error :)
get_leaves_attr(dend, "label")
#> [1] "Arizona" "Alabama" "Alaska" 
labels(dend, "label")
#> [1] "Arizona" "Alabama" "Alaska" 
get_leaves_attr(dend, "height") # should be 0's
#> [1] 0 0 0
get_nodes_attr(dend, "height")
#> [1] 54.80041  0.00000 37.17701  0.00000  0.00000

get_leaves_attr(dend, "nodePar")
#> NULL


get_leaves_attr(dend, "leaf") # should be TRUE's
#> [1] TRUE TRUE TRUE
get_nodes_attr(dend, "leaf") # conatins NA's
#> [1]   NA TRUE   NA TRUE TRUE


get_leaves_attr(dend, "members") # should be 1's
#> [1] 1 1 1
get_nodes_attr(dend, "members") #
#> [1] 3 1 2 1 1


get_leaves_attr(dend, "members", simplify = FALSE) # should be 1's
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 1
#> 
#> [[2]][[2]]
#> [1] 1
#> 
#>