Convert a tax_table into a data.frame with numeric and list columns
Source: R/tax_table_to_df.R
tax_table_to_df.RdThe tax_table slot of a phyloseq object is stored as a character
matrix, so numeric information (e.g. a bootstrap support, a trait value)
is silently coerced to text and list-valued cells cannot be represented
at all. This helper lifts those limitations by returning a tibble in
which columns that look numeric (or logical) are converted to their
natural type, and selected columns can be split into genuine list
columns.
It does not modify the phyloseq object; it produces a stand-alone tibble for downstream (tidyverse) manipulation.
Arguments
- physeq
(phyloseq or taxonomyTable, required) A phyloseq object with a
tax_tableslot, or atax_tableobject directly.- convert
(logical, default TRUE) If TRUE, each column is passed through
utils::type.convert()(withas.is = TRUE) so that columns holding only numbers become numeric/integer and"TRUE"/"FALSE"columns become logical. Columns with any non-numeric value are kept as character.- split
(character, optional) Names of columns to turn into list columns by splitting each cell on
sep. Useful when a rank cell packs several values (e.g. multiple GBIF matches).- sep
(character, default
"/") Separator used to split the columns named insplit. Passed tobase::strsplit()withfixed = TRUE.- taxa_names_col
(character or NULL, default
"taxon") Name of the leading column holding the taxa names. If NULL, taxa names are kept as row names instead of a column.
Value
A tibble::tibble with one row per taxon and one column per
taxonomic rank, plus (unless taxa_names_col is NULL) a leading column
of taxa names.
Examples
data_fungi_mini <-
tidypq::mutate_taxa_pq(data_fungi_mini,
Mol_Abundance = taxa_sums(.))
# Numeric-looking ranks are converted to numeric
df <- tax_table_to_df(data_fungi_mini)
df |>
group_by(Class) |>
summarize(mean=mean(Mol_Abundance))
#> # A tibble: 4 × 2
#> Class mean
#> <chr> <dbl>
#> 1 Agaricomycetes 13105.
#> 2 Atractiellomycetes 11053
#> 3 Tremellomycetes 7750.
#> 4 NA 5675
if (FALSE) { # \dontrun{
# Split a packed column into a list column
tax_table_to_df(data_fungi_mini, split = "Genus", sep = "_")
} # }