Learn R Programming

rbiom (version 2.2.0)

subset: Subset an rbiom object by sample names, OTU names, metadata, or taxonomy.

Description

Dropping samples or OTUs will lead to observations being removed from the OTU matrix (biom$counts). OTUs and samples with zero observations are automatically removed from the rbiom object.

Usage

# S3 method for rbiom
subset(x, subset, clone = TRUE, ...)

# S3 method for rbiom [(x, i, j, ..., clone = TRUE, drop = FALSE)

# S3 method for rbiom na.omit(object, fields = ".all", clone = TRUE, ...)

subset_taxa(x, subset, clone = TRUE, ...)

Value

An rbiom object.

Arguments

x

An rbiom object, such as from as_rbiom().

subset

Logical expression for rows to keep. See base::subset().

clone

Create a copy of biom before modifying. If FALSE, biom is modified in place as a side-effect. See speed ups for use cases. Default: TRUE

...

Not used.

i, j

The sample or OTU names to keep. Or a logical/integer vector indicating which sample names from biom$samples or biom$otus to keep. Subsetting with [i] takes i as samples, whereas [i,j] takes i as otus and j as samples (corresponding to [rows, cols] in the underlying biom$counts matrix).

drop

Not used

object

An rbiom object, such as from as_rbiom().

fields

Which metadata field(s) to check for NAs, or ".all" to check all metadata fields.

See Also

Other transformations: modify_metadata, rarefy(), rarefy_cols(), slice_metadata, with()

Examples

Run this code
    library(rbiom)
    library(dplyr)
    
    # Subset to specific samples
    biom <- hmp50[c('HMP20', 'HMP42', 'HMP12')]
    biom$metadata
    
    # Subset to specific OTUs
    biom <- hmp50[c('LtbAci52', 'UncO2012'),] # <- Trailing ,
    biom$taxonomy
    
    # Subset to specific samples and OTUs
    biom <- hmp50[c('LtbAci52', 'UncO2012'), c('HMP20', 'HMP42', 'HMP12')]
    as.matrix(biom)
    
    # Subset samples according to metadata
    biom <- subset(hmp50, `Body Site` %in% c('Saliva') & Age < 25)
    biom$metadata
    
    # Subset OTUs according to taxonomy
    biom <- subset_taxa(hmp50, Phylum == 'Cyanobacteria')
    biom$taxonomy
    
    # Remove samples with NA metadata values
    biom <- mutate(hmp50, BS2 = na_if(`Body Site`, 'Saliva'))
    biom$metadata
    biom <- na.omit(biom)
    biom$metadata

Run the code above in your browser using DataLab