log_transform: Log transformation of a matrix

View source: R/sparse_matrix_functions.R

log_transformR Documentation

Log transformation of a matrix

Description

Log transforms a matrix after adding 1, or the reverse, exponentiates the matrix and then subtracts 1. In the case of sparse matrices (class dgTMatrix, dgCMatrix or dgRMatrix), only the non-zero elements are transformed. This is faster than manually adding or subtracting 1, as it retains the sparse matrix class.

Usage

log_transform(mat, base = 2, reverse = FALSE)

Arguments

mat

A matrix. May be sparse (i.e. of class dgTMatrix, dgCMatrix or dgRMatrix).

base

A positive number: the base with respect to which the logarithms are computed. Default: 2.

reverse

If FALSE, log transform mat; if TRUE, reverse log transform, i.e. exponentiate mat.

Value

A matrix of the same size and class as mat.

Examples

mat <- matrix(round(runif(100, max = 100)), 10, 10)
log_transform(mat)
log_transform(log_transform(mat), reverse = TRUE)
mat[sample(1:100, 80)] <- 0
mat <- as(mat, 'dgCMatrix')
log_transform(mat)

m20ty/matkot documentation built on July 1, 2023, 8:11 p.m.