merge_samples2: Merge samples by a sample variable or factor

merge_samples2R Documentation

Merge samples by a sample variable or factor

Description

This function provides an alternative to phyloseq::merge_samples() that better handles sample variables of different types, especially categorical sample variables. It combines the samples in x defined by the sample variable or factor group by summing the abundances in otu_table(x) and combines sample variables by the summary functions in funs. The default summary function, unique_or_na(), collapses the values within a group to a single unique value if it exists and otherwise returns NA. The new (merged) samples are named by the values in group.

Usage

merge_samples2(x, group, fun_otu = sum, funs = list(), reorder = FALSE)

## S4 method for signature 'phyloseq'
merge_samples2(x, group, fun_otu = sum, funs = list(), reorder = FALSE)

## S4 method for signature 'otu_table'
merge_samples2(x, group, fun_otu = sum, reorder = FALSE)

## S4 method for signature 'sample_data'
merge_samples2(x, group, funs = list(), reorder = FALSE)

Arguments

x

A phyloseq, otu_table, or sample_data object

group

A sample variable or a vector of length nsamples(x) defining the sample grouping. A vector must be supplied if x is an otu_table

fun_otu

Function for combining abundances in the otu table; default is sum. Can be a formula to be converted to a function by purrr::as_mapper()

funs

Named list of merge functions for sample variables; default is unique_or_na

reorder

Logical specifying whether to reorder the new (merged) samples by name

Examples

data(enterotype)

# Merge samples with the same project and clinical status
ps <- enterotype 
sample_data(ps) <- sample_data(ps) %>%
  transform(Project.ClinicalStatus = Project:ClinicalStatus)
sample_data(ps) %>% head
ps0 <- merge_samples2(ps, "Project.ClinicalStatus",
  fun_otu = mean,
  funs = list(Age = mean)
)
sample_data(ps0) %>% head

mikemc/speedyseq documentation built on April 22, 2024, 6:40 p.m.