dend_node_apply: Apply functions on every node in a dendrogram

Description Usage Arguments Details Value Examples

View source: R/dend_utils.R

Description

Apply functions on every node in a dendrogram

Usage

1
dend_node_apply(dend, fun)

Arguments

dend

A dendrogram.

fun

A self-defined function.

Details

The function returns a vector or a list as the same length as the number of nodes in the dendrogram.

The self-defined function can have one single argument which is the sub-dendrogram at a certain node. E.g. to get the number of members at every node:

1
    dend_node_apply(dend, function(d) attr(d, "members"))  

The self-defined function can have a second argument, which is the index of current sub-dendrogram in the complete dendrogram. E.g. dend[[1]] is the first child node of the complete dendrogram and dend[[c(1, 2)]] is the second child node of dend[[1]], et al. This makes that at a certain node, it is possible to get information of its child nodes and parent nodes.

1
2
3
4
5
    dend_node_apply(dend, function(d, index) {
        dend[[c(index, 1)]] # is the first child node of d, or simply d[[1]]
        dend[[index[-length(index)]]] # is the parent node of d
        ...
    })  

Note for the top node, the value of index is NULL.

Value

A vector or a list, depends on whether fun returns a scalar or more complex values.

Examples

1
2
3
4
5
6
mat = matrix(rnorm(100), 10)
dend = as.dendrogram(hclust(dist(mat)))
# number of members on every node
dend_node_apply(dend, function(d) attr(d, "members"))
# the depth on every node
dend_node_apply(dend, function(d, index) length(index))

simplifyEnrichment documentation built on Nov. 8, 2020, 5:07 p.m.