Skip to contents

Rarefaction subset counts so that all samples have the same number of observations. Rescaling rows or cols scales the matrix values so that row sums or column sums equal 1.

Usage

rarefy_cols(mtx, depth = 0.1, n = NULL, seed = 0)

rescale_cols(mtx)

rescale_rows(mtx)

Arguments

mtx

A matrix-like object.

depth

How many observations to keep per sample. When 0 < depth < 1, it is taken as the minimum percentage of the dataset's observations to keep. Ignored when n is specified. Default: 0.1

n

The number of samples to keep. When 0 < n < 1, it is taken as the percentage of samples to keep. If negative, that number or percentage of samples is dropped. If 0, all samples are kept. If NULL, depth is used instead. Default: NULL

seed

An integer to use for seeding the random number generator. If you need to create different random rarefactions of the same matrix, set this seed value to a different number each time.

Value

The rarefied or rescaled matrix.

See also

Other rarefaction: rare_corrplot(), rare_multiplot(), rare_stacked(), rarefy(), sample_sums()

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

Examples

    library(rbiom)
    
    # rarefy_cols --------------------------------------
    biom <- hmp50$clone()
    sample_sums(biom) %>% head(10)
#> HMP01 HMP02 HMP03 HMP04 HMP05 HMP06 HMP07 HMP08 HMP09 HMP10 
#>  1660  1371  1353  1895  3939  4150  3283  1695  2069  2509 

    biom$counts %<>% rarefy_cols(depth=1000)
    sample_sums(biom) %>% head(10)
#> HMP01 HMP02 HMP03 HMP04 HMP05 HMP06 HMP07 HMP08 HMP09 HMP10 
#>  1000  1000  1000  1000  1000  1000  1000  1000  1000  1000 
    
    
    # rescaling ----------------------------------------
    mtx <- matrix(sample(1:20), nrow=4)
    mtx
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   18    9    4   16   14
#> [2,]    1   17   20    7    5
#> [3,]    6   10    8   19    3
#> [4,]   15   12   11    2   13
    
    rowSums(mtx)
#> [1] 61 50 46 53
    rowSums(rescale_rows(mtx))
#> [1] 1 1 1 1
    
    colSums(mtx)
#> [1] 40 48 43 44 35
    colSums(rescale_cols(mtx))
#> [1] 1 1 1 1 1