phyloseq (version 1.16.2)

merge_samples: Merge samples based on a sample variable or factor.

Description

The purpose of this method is to merge/agglomerate the sample indices of a phyloseq object according to a categorical variable contained in a sample_data or a provided factor.

Usage

merge_samples(x, group, fun=mean)

## S3 method for class 'sample_data': merge_samples(x, group, fun = mean)

## S3 method for class 'otu_table': merge_samples(x, group)

## S3 method for class 'phyloseq': merge_samples(x, group, fun = mean)

Arguments

x
(Required). An instance of a phyloseq class that has sample indices. This includes sample_data-class, otu_table-class, and phyloseq-class.
group
(Required). Either the a single character string matching a variable name in the corresponding sample_data of x, or a factor with the same length as the number of samples in x.
fun
(Optional). The function that will be used to merge the values that correspond to the same group for each variable. It must take a numeric vector as first argument and return a single value. Default is mean. Note that this is (currently) ignored for the otu_table, where the equivalent function is sum, but evaluated via rowsum for efficiency.

Value

  • A phyloseq object that has had its sample indices merged according to the factor indicated by the group argument. The output class matches x.

Details

NOTE: (phylo) trees and taxonomyTable-class are not modified by this function, but returned in the output object as-is.

See Also

merge_taxa, code{merge_phyloseq}

Examples

Run this code
#
data(GlobalPatterns)
GP = GlobalPatterns
mergedGP = merge_samples(GlobalPatterns, "SampleType")
SD = merge_samples(sample_data(GlobalPatterns), "SampleType")
print(SD)
print(mergedGP)
sample_names(GlobalPatterns)
sample_names(mergedGP)
identical(SD, sample_data(mergedGP))
# The OTU abundances of merged samples are summed
# Let's investigate this ourselves looking at just the top10 most abundance OTUs...
OTUnames10 = names(sort(taxa_sums(GP), TRUE)[1:10])
GP10  = prune_taxa(OTUnames10,  GP)
mGP10 = prune_taxa(OTUnames10, mergedGP)
ocean_samples = sample_names(subset(sample_data(GP), SampleType=="Ocean"))
print(ocean_samples)
otu_table(GP10)[, ocean_samples]
rowSums(otu_table(GP10)[, ocean_samples])
otu_table(mGP10)["Ocean", ]

Run the code above in your browser using DataLab