Learn R Programming

ggphylo (version 0.1)

tree.foreach: Loops over nodes in the tree, applying a function using each node as an argument. Optionally, either the leaves or internal nodes can be dropped from the iteration.

Description

Loops over nodes in the tree, applying a function using each node as an argument. Optionally, either the leaves or internal nodes can be dropped from the iteration.

Usage

tree.foreach(phylo, fn, leaves = TRUE, nodes = TRUE)

Arguments

phylo
input phylo object
fn
the function to call. For each node, fn will be called with arguments [phylo, node] where node is the node's index within the phylo object.

Examples

Run this code
set.seed(1)
tree <- rcoal(3)
str <- ''
tree.foreach(tree, function(x, i) {
  str <<- paste(str, ' ', tree.get.label(x, i), '=', tree.depth.to.root(x, i), sep='')
})
print(str)

Run the code above in your browser using DataLab