| rarefy | R Documentation |
Sub-sample OTU observations such that all samples have an equal number.
If called on data with non-integer abundances, values will be re-scaled to
integers between 1 and depth such that they sum to depth.
rarefy(
counts,
depth = 0.1,
n_samples = NULL,
seed = 0,
times = NULL,
drop = TRUE,
margin = 1L,
cpus = n_cpus()
)
counts |
A numeric matrix of count data where each column is a
feature, and each row is a sample. Any object coercible with
|
depth |
How many observations to keep per sample. When
|
n_samples |
The number of samples to keep. When |
seed |
An integer seed for randomizing which observations to keep or
drop. If you need to create different random rarefactions of the same
data, set the seed to a different number each time. Default: |
times |
How many independent rarefactions to perform. If set,
|
drop |
Drop rows and columns with zero observations after rarefying.
Default: |
margin |
If your samples are in the matrix's rows, set to |
cpus |
How many parallel processing threads should be used. The
default, |
A rarefied matrix. Matrix and slam objects will be returned with
the same type; otherwise a base R matrix will be returned.
# A 4-sample x 5-OTU matrix with samples in rows.
counts <- matrix(c(0,0,0,0,0,8,9,10,5,5,5,5,2,0,0,0,6,5,7,0), 4, 5,
dimnames = list(LETTERS[1:4], paste0('OTU', 1:5)))
counts
rowSums(counts)
# Rarefy all samples to a depth of 13.
# Note that sample 'A' has 0 counts and is dropped.
r_mtx <- rarefy(counts, depth = 13, seed = 1)
r_mtx
rowSums(r_mtx)
# Keep zero-sum rows and columns by setting `drop = FALSE`.
rarefy(counts, depth = 13, drop = FALSE, seed = 1)
# Rarefy to the depth of the 2nd most abundant sample (B, depth=22).
rarefy(counts, n_samples = 2, seed = 1)
# Perform 3 independent rarefactions.
r_list <- rarefy(counts, depth = 13, times = 3, seed = 1)
length(r_list)
r_list[[1]]
# The class of the input matrix is preserved.
if (requireNamespace('Matrix', quietly = TRUE)) {
counts_dgC <- Matrix::Matrix(counts, sparse = TRUE)
class(counts_dgC)
r_dgC <- rarefy(counts_dgC, depth = 13, seed = 1)
class(r_dgC)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.