Description Usage Arguments Details Value Author(s) Examples
Center and scale a matrix using given vectors for centering and scaling.
1 |
x |
A numeric matrix, preferable with storage mode double as the calculations might be faster. |
center |
Logical or a numeric vector (length one or |
scale |
Logical or a numeric vector (length one or |
inplace |
Logical. If |
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
).
Either a modified copy of the original matrix if
inplace = FALSE
, or nothing if inplace = TRUE
.
Dominik Mueller
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)
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.