Coerce a taxonomy data.frame to various formats

as.taxo(x)

is.taxo(x)

# S3 method for taxo
as.list(x, ...)

# S3 method for taxo
as.Node(x, ...)

Arguments

x

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

...

passed to other methods.

Details

A taxonomy data.frame is just a regular data.frame with columns id, parent_id, and name. The links between the id and parent_id define the taxonomic hierarchy. It can be converted into an actual tree using data.tree::as.Node() and into a nested list.

See also

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

Examples

taxo
#> id parent_id name #> 1 1 NA living #> 2 2 1 fish #> 3 3 1 mollusc #> 4 4 2 egg #> 5 5 3 snail #> 6 6 3 squid #> 7 7 5 egg
as.Node(taxo)
#> levelName #> 1 # #> 2 °--living #> 3 ¦--fish #> 4 ¦ °--egg #> 5 °--mollusc #> 6 ¦--snail #> 7 ¦ °--egg #> 8 °--squid
print(as.Node(taxo), "id", "parent_id")
#> levelName id parent_id #> 1 # NA NA #> 2 °--living 1 NA #> 3 ¦--fish 2 1 #> 4 ¦ °--egg 4 2 #> 5 °--mollusc 3 1 #> 6 ¦--snail 5 3 #> 7 ¦ °--egg 7 5 #> 8 °--squid 6 3
as.list(taxo)
#> $living #> $living$fish #> $living$fish$egg #> [1] "" #> #> #> $living$mollusc #> $living$mollusc$squid #> [1] "" #> #> $living$mollusc$snail #> $living$mollusc$snail$egg #> [1] "" #> #> #> #> #> attr(,"class") #> [1] "taxo_list" "list"
# transforming into a taxonomy data.frame is just a mater of adding a class df <- taxo class(df)
#> [1] "taxo" "data.frame"
is.taxo(df)
#> [1] TRUE
df <- as.data.frame(df) class(df)
#> [1] "data.frame"
is.taxo(df)
#> [1] FALSE
df <- as.taxo(df) class(df)
#> [1] "taxo" "data.frame"
is.taxo(df)
#> [1] TRUE
if (FALSE) { db <- db_connect_ecotaxa() taxo <- extract_taxo(db, c(8000, 20000)) as.Node(taxo) }