col_scale: Center and scale a matrix.

Description Usage Arguments Details Value Author(s) Examples

Description

Center and scale a matrix using given vectors for centering and scaling.

Usage

1
col_scale(x, center = FALSE, scale = FALSE, inplace = FALSE)

Arguments

x

A numeric matrix, preferable with storage mode double as the calculations might be faster.

center

Logical or a numeric vector (length one or ncol(x) being subtracted from the columns of x for centering.

scale

Logical or a numeric vector (length one or ncol(x) to divide the columns of x for scaling.

inplace

Logical. If TRUE, the matrix is modified in memory, i.e., no copy is being made. This is faster and saves memory, but only possible if storage mode is double. If FALSE, a copy is made, modified and returned.

Details

Arguments to center and scale can be either logical or numeric vectors. If TRUE, centering is done by column means and scaling by the sample standard deviation of the columns. If arguments are numerical, they must be either of length one (recycled to the number of columns of x) or of length equal to ncol(x). Arguments to center and scale are independent.

This function was specifically desiged for scaling with known center and scale arguments. Note that maximum speed is achieved if the matrix has storage mode double and is modified in memory (inplace = TRUE).

Value

Either a modified copy of the original matrix if inplace = FALSE, or nothing if inplace = TRUE.

Author(s)

Dominik Mueller

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
x <- matrix(as.double(1:20), nrow = 4)
col_scale(x, center = 4, scale = TRUE)

x <- matrix(rnorm(1e6L), nrow = 1e2L, ncol = 1e4L)
center <- colMeans(x)
scaling <- matrixStats::colSds(x, center = center)
xx <- x + 0.0
xx_ <- x + 0.0

microbenchmark::microbenchmark(times = 10L,
 scale(x, center = center, scale = scaling),
 scale(xx, center = center, scale = scaling),
 col_scale(x, center, scaling),
 col_scale(xx, center, scaling),
 col_scale(xx, center, scaling, inplace = TRUE)
)

DominikMueller64/dmisc documentation built on May 6, 2019, 2:52 p.m.