merge_samples {phyloseq} | R Documentation

## 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)

    ## S4 method for signature 'sample_data'
    merge_samples(x, group, fun = mean)

    ## S4 method for signature 'otu_table'
    merge_samples(x, group)

    ## S4 method for signature '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.

### Details

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

### 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`.

### See Also

`merge_taxa`, codemerge_phyloseq

### Examples

    #
    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", ]

[Package phyloseq version 1.34.0 Index]