Learn R Programming

neuroim (version 0.0.6)

splitScale: Generic function to center/scale row-subsets of a matrix or matrix-like object

Description

Generic function to center/scale row-subsets of a matrix or matrix-like object

Usage

splitScale(x, f, center, scale)
"splitScale"(x, f, center = TRUE, scale = TRUE)
"splitScale"(x, f)

Arguments

x
a numeric matrix or matrix-like object
f
the splitting object, typically a factor or set of integer indices. must be equal to number of rows of matrix.
center
should values within each submatrix be centered? (mean removed from each column of submatrix)
scale
should values be scaled? (divide vector by standard deviation from each column of submatrix)

Value

a new matrix or matrix-like object where the original rows have been grouped by f and then centered and/or scaled for each grouping

Examples

Run this code

M <- matrix(rnorm(1000), 10, 100)
fac <- factor(rep(1:2, each=5))
Ms <- splitScale(M, fac)

## correctly centered
all(abs(apply(Ms[fac == 1,], 2, mean)) < .000001)
all(abs(apply(Ms[fac == 2,], 2, mean)) < .000001)

# correctly scaled
all.equal(apply(Ms[fac == 1,], 2, sd), rep(1, ncol(Ms)))
all.equal(apply(Ms[fac == 2,], 2, sd), rep(1, ncol(Ms)))

Run the code above in your browser using DataLab