Fix members attr in a dendrogram after (for example), the tree was pruned or manipulated.
fix_members_attr.dendrogram(dend, ...)
A dendrogram, after adjusting the members attr in all of its nodes.
# define dendrogram object to play with:
hc <- hclust(dist(USArrests[1:3, ]), "ave")
dend <- as.dendrogram(hc)
# plot(dend)
# prune one leaf
dend[[2]] <- dend[[2]][[1]]
# plot(dend)
dend # but it is NO LONGER true that it has 3 members total!
#> 'dendrogram' with 2 branches and 3 members total, at height 54.80041
fix_members_attr.dendrogram(dend) # it now knows it has only 2 members :)
#> 'dendrogram' with 2 branches and 2 members total, at height 54.80041
hc <- hclust(dist(USArrests[1:3, ]), "ave")
dend <- as.dendrogram(hc)
identical(prune_leaf(dend, "Alaska"), fix_members_attr.dendrogram(prune_leaf(dend, "Alaska")))
#> [1] FALSE
str(unclass(prune_leaf(dend, "Alaska")))
#> List of 2
#> $ : int 3
#> ..- attr(*, "members")= int 1
#> ..- attr(*, "height")= num 0
#> ..- attr(*, "label")= chr "Arizona"
#> ..- attr(*, "leaf")= logi TRUE
#> $ : int 1
#> ..- attr(*, "label")= chr "Alabama"
#> ..- attr(*, "members")= int 1
#> ..- attr(*, "height")= num 0
#> ..- attr(*, "leaf")= logi TRUE
#> - attr(*, "members")= num 2
#> - attr(*, "midpoint")= num 0.5
#> - attr(*, "height")= num 54.8
str(unclass(fix_members_attr.dendrogram(prune_leaf(dend, "Alaska"))))
#> List of 2
#> $ : int 3
#> ..- attr(*, "members")= int 1
#> ..- attr(*, "height")= num 0
#> ..- attr(*, "label")= chr "Arizona"
#> ..- attr(*, "leaf")= logi TRUE
#> $ : int 1
#> ..- attr(*, "label")= chr "Alabama"
#> ..- attr(*, "members")= int 1
#> ..- attr(*, "height")= num 0
#> ..- attr(*, "leaf")= logi TRUE
#> - attr(*, "members")= int 2
#> - attr(*, "midpoint")= num 0.5
#> - attr(*, "height")= num 54.8