split_matrix_col: Split a matrix in a list of matrices by levels of a factor /...

View source: R/split-matrix-methods.R

split_matrix_colR Documentation

Split a matrix in a list of matrices by levels of a factor / integer

Description

This function also works with SummarizedExperiment::SummarizedExperiment() / SingleCellExperiment::SingleCellExperiment()

Usage

split_matrix_col(m, i)

lapply_attr(X, FUN, ...)

split_matrix_row(m, i)

unsplit_matrix(x)

Arguments

m

matrix-like

i

factor-like

X

a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list.

FUN

the function to be applied to each element of X: see ‘Details’. In the case of functions like +, %*%, the function name must be backquoted or quoted.

...

optional arguments to FUN.

x

a list, typically returned from lapply().

Value

list of matrices of length(unique(i))

Functions

  • lapply_attr: lapply and preserve attributes. Necessary for unsplit_matrix.

  • split_matrix_row: split a matrix by row

  • unsplit_matrix: reverse a split (cbind/rbind) and reordering

Examples

m = matrix(1:12, nrow = 3)
s_col = split_matrix_col(m, c('A', 'A', 'B', 'B'))
s_row = split_matrix_row(m, c('B', 'A', 'B'))
scaled = lapply_attr(s_col, function(x) x/sum(x))
unsplit_matrix(scaled)
stopifnot(all.equal(unsplit_matrix(s_row), m))

amcdavid/Genesee documentation built on April 14, 2022, 5:16 a.m.