Get the ids of all ancestors of some taxa

ancestors(taxon_ids, taxo, n = Inf)

Arguments

taxon_ids

internal, numeric ids of the taxa.

taxo

a taxonomy data.frame, typically from extract_taxo().

n

maximum number of levels to look up; n=1 gives the parents, n=2 gives the parents and the grand-parents, etc.

Value

A vector of taxonomic ids containing the input taxon_ids and all their ancestors. With a single taxon in taxon_ids, the ancestors are in ordered from the taxon to the root of the tree: they define the lineage. When taxon_ids contains multiple taxa, the order is non-trivial.

Details

Even with n=1, the function is different from parent() because it returns all unique parents for all elements of taxon_ids, not one parent per element.

See also

Other taxonomy-related functions: as.taxo(), children(), descendants(), extract_taxo(), is_leaf(), lineage(), parent(), taxo_id(), taxo_name()

Examples

print(as.Node(taxo), "id")
#> levelName id #> 1 # NA #> 2 °--living 1 #> 3 ¦--fish 2 #> 4 ¦ °--egg 4 #> 5 °--mollusc 3 #> 6 ¦--snail 5 #> 7 ¦ °--egg 7 #> 8 °--squid 6
ancestors(5, taxo)
#> [1] 1 3 5
ancestors(1, taxo)
#> [1] 1
ancestors(NA, taxo)
#> [1] NA
ancestors(1:7, taxo)
#> [1] 1 3 2 5 4 6 7
ancestors(6, taxo, n=1)
#> [1] 3 6
ancestors(6, taxo, n=2)
#> [1] 1 3 6
ancestors(6, taxo, n=3)
#> [1] 1 3 6
ancestors(6, taxo, n=10)
#> [1] 1 3 6
# NB: ancestors(5:6, taxo, n=1)
#> [1] 3 5 6
parent(5:6, taxo)
#> [1] 3 3