Apply a function to the leaves of each taxon. This is similar to using leaves() with lapply() or sapply().

obj$leaves_apply(func, subset = NULL, recursive = TRUE,
  simplify = FALSE, value = "taxon_indexes", ...)
leaves_apply(obj, func, subset = NULL, recursive = TRUE,
  simplify = FALSE, value = "taxon_indexes", ...)

Arguments

obj

The taxonomy() or taxmap() object containing taxon information to be queried.

func

(function) The function to apply.

subset

Taxon IDs, TRUE/FALSE vector, or taxon indexes to use. Default: All taxa in obj will be used. Any variable name that appears in all_names() can be used as if it was a vector on its own.

recursive

(logical or numeric) If FALSE, only return the leaves if they occur one rank below the target taxa. If TRUE, return all of the leaves for each taxon. Positive numbers indicate the number of recursions (i.e. number of ranks below the target taxon to return). 1 is equivalent to FALSE. Negative numbers are equivalent to TRUE.

simplify

(logical) If TRUE, then combine all the results into a single vector of unique values.

value

What data to give to the function. Any result of all_names(obj) can be used, but it usually only makes sense to use data that has an associated taxon id.

...

Extra arguments are passed to the function func.

Examples

# Count number of leaves under each taxon or its subtaxa leaves_apply(ex_taxmap, length)
#> $b #> [1] 4 #> #> $c #> [1] 2 #> #> $d #> [1] 2 #> #> $e #> [1] 1 #> #> $f #> [1] 1 #> #> $g #> [1] 2 #> #> $h #> [1] 1 #> #> $i #> [1] 1 #> #> $j #> [1] 1 #> #> $k #> [1] 1 #> #> $l #> [1] 2 #> #> $m #> [1] 0 #> #> $n #> [1] 0 #> #> $o #> [1] 0 #> #> $p #> [1] 0 #> #> $q #> [1] 0 #> #> $r #> [1] 0 #>
# Count number of leaves under each taxon leaves_apply(ex_taxmap, length, recursive = FALSE)
#> $b #> [1] 0 #> #> $c #> [1] 0 #> #> $d #> [1] 0 #> #> $e #> [1] 0 #> #> $f #> [1] 0 #> #> $g #> [1] 0 #> #> $h #> [1] 1 #> #> $i #> [1] 1 #> #> $j #> [1] 1 #> #> $k #> [1] 1 #> #> $l #> [1] 2 #> #> $m #> [1] 0 #> #> $n #> [1] 0 #> #> $o #> [1] 0 #> #> $p #> [1] 0 #> #> $q #> [1] 0 #> #> $r #> [1] 0 #>
# Converting output of leaves to upper case leaves_apply(ex_taxmap, value = "taxon_names", toupper)
#> $b #> m n o p #> "TIGRIS" "CATUS" "TYPHLOPS" "SAPIENS" #> #> $c #> q r #> "LYCOPERSICUM" "TUBEROSUM" #> #> $d #> m n #> "TIGRIS" "CATUS" #> #> $e #> o #> "TYPHLOPS" #> #> $f #> p #> "SAPIENS" #> #> $g #> q r #> "LYCOPERSICUM" "TUBEROSUM" #> #> $h #> m #> "TIGRIS" #> #> $i #> n #> "CATUS" #> #> $j #> o #> "TYPHLOPS" #> #> $k #> p #> "SAPIENS" #> #> $l #> q r #> "LYCOPERSICUM" "TUBEROSUM" #> #> $m #> named character(0) #> #> $n #> named character(0) #> #> $o #> named character(0) #> #> $p #> named character(0) #> #> $q #> named character(0) #> #> $r #> named character(0) #>
# Passing arguments to the function leaves_apply(ex_taxmap, value = "taxon_names", paste0, collapse = ", ")
#> $b #> [1] "tigris, catus, typhlops, sapiens" #> #> $c #> [1] "lycopersicum, tuberosum" #> #> $d #> [1] "tigris, catus" #> #> $e #> [1] "typhlops" #> #> $f #> [1] "sapiens" #> #> $g #> [1] "lycopersicum, tuberosum" #> #> $h #> [1] "tigris" #> #> $i #> [1] "catus" #> #> $j #> [1] "typhlops" #> #> $k #> [1] "sapiens" #> #> $l #> [1] "lycopersicum, tuberosum" #> #> $m #> [1] "" #> #> $n #> [1] "" #> #> $o #> [1] "" #> #> $p #> [1] "" #> #> $q #> [1] "" #> #> $r #> [1] "" #>