Collapses an abundance table by a specified taxonomic rank (e.g. genus, family, phylum), summing counts across features that share the same taxonomy. Features with lower taxonomic resolution than the selected level are retained unchanged. Optionally converts counts to relative abundance and exports the collapsed table to a tab-delimited file.

collapse_table(
  table,
  metadata,
  level = "genus",
  rel_abun = FALSE,
  export_txt = FALSE,
  file_name = "collapsed_table.txt"
)

Arguments

table

A data frame containing an OTU/ASV abundance table. Must include a column with OTUID, one with taxonomy and sample columns with numeric counts.

metadata

A data frame containing sample metadata. The first column is used to define the order of samples in the output table.

level

Character string specifying the taxonomic level to collapse to. ("kingdom","phylum","class","order","family","genus" or"species".

rel_abun

Logical. If TRUE, converts counts to relative abundance (%) per sample.

export_txt

Logical. If TRUE, exports the collapsed table as a tab-delimited text file.

file_name

Character. Name of the output file when export_txt = TRUE.

Value

a table collapsed by taxonomic level

Examples

collapse_table(
  table = table,
  metadata = metadata,
  level = "genus",
  rel_abun = FALSE,
  export_txt = FALSE
)
#> Error in table[, c("taxonomy", setdiff(names(table), "taxonomy"))]: object of type 'closure' is not subsettable