center_matrix: Centers a matrix

Description Usage Arguments Value Author(s) Examples

View source: R/preprocess.R

Description

Centers the mean/median of each row/column of the given matrix around zero.

Usage

1
center_matrix(x, by = "row", method = "mean", scale = FALSE, verbose = FALSE)

Arguments

x

a numeric matrix or Matrix object.

by

either "row" or "col". Default is "row".

method

either "mean" or "median". Default is "mean".

scale

logical indicating whether to scale the rows/columns of x, i.e. divide each row/column by the standard deviation of the row/column. Default is FALSE.

verbose

suppresses all messages from this function. Default is FALSE.

Value

A matrix with either mean or median of row/column centered around zero.

Author(s)

Avishay Spitzer

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Center the mean of each row around zero
m <- matrix(runif(25, 0, 100), nrow = 5, ncol = 5) # Generate a 5x5 numeric matrix
m <- center_matrix(m, by = "row", method = "mean", scale = FALSE) # Center
all(rowMeans(m) == 0) # TRUE
all(colMeans(m) == 0) # FALSE

# Center the median of each column around zero
m <- matrix(runif(25, 0, 100), nrow = 5, ncol = 5) # Generate a 5x5 numeric matrix
m <- center_matrix(m, by = "col", method = "median", scale = FALSE) # Center
all(apply(m, 1, median) == 0)  # FALSE
all(apply(m, 2, median) == 0)  # FALSE

dravishays/scandal documentation built on Jan. 8, 2020, 1:30 p.m.