tree_fun: Apply a function recursively through the tree

Description Usage Arguments Examples

View source: R/prep_data.R

Description

Given a value associated with each tip in a tree, this calculates, for each internal node, the sum across all tips that descend from it.

Usage

1
tree_fun(el, values, f)

Arguments

el

[character matrix] The edgelist specifying the tree structure. The first column are character names for the parents, the second are children.

values

[named vector] Tip values on which to aggregate. The names must be the same names as in el.

f

[function] The function to apply to each group of children values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
el <- data.frame(
  parent = c("1", "1","2", "2", "2"),
  child = c("2", "7", "3", "4", "5"),
  stringsAsFactors = FALSE
)
counts  <- c(
  "7" = 10,
  "3" = 2,
  "4" = 5,
  "5" = 1
)
tree_sum(el, counts)

krisrs1128/treelapse documentation built on Jan. 3, 2020, 6:29 p.m.