tree_apply: Apply a function to each node of a tree

Description Usage Arguments Examples

Description

tree_apply applies a function fun to each node of .tree and stores the results in the attribute at.

Usage

1
2
3
4
tree_apply(.tree, ...)

## S3 method for class 'rtree'
tree_apply(.tree, at, fun, ...)

Arguments

.tree

A tree.

...

Additional arguments to be passed to fun.

at

character. Name of the attribute to be created at each node of .tree that will contain the result of fun.

fun

function or character. A function taking two arguments .node and .tree (in this order), to be applied to each node of the tree.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Rooted tree
(tr0 = c_("Bob", "Carl", "Daniel"))
(tr1 = c_("Bill", "Caroline", "Dimitri", "Enoc"))
(tr2 = r_("Alice", s = list(tr0, tr1)))

## Unrooted tree
(tr3 = r_(s = list(tr2, c_("Grand-Mother", "Father", "Son"))))

f <- function(.node, .tree) nchar(label(.node))
tr4 = tree_apply(tr3, at = "value", fun = f)
print(tr4, at = "value")

g <- function(.node, .tree) height(take_branch(.tree, .node))
tr5 = tree_apply(tr3, at = "height", fun = g)
print(tr5, at = "height")

paulponcet/oak documentation built on May 14, 2019, 7:21 p.m.