| matrixStats | R Documentation |
These two functions are fast C++ versions for column- and row-wise variance calculation on matrices/data.frames and are meant to substitute the classical apply(mat, 1, var) approach.
colVarsC(x)
rowVarsC(x)
x |
a matrix or data.frame |
They are coded in a way that they automatically remove NA values, so they behave like na.rm = TRUE; a column/row with fewer than two non-missing values gives NaN. Both functions require x to have at least two columns: for a single column, colVarsC stops with "'x' should have more than one column!" and rowVarsC with "'x' should have more than one column!".
A (named) vector of column/row variances, with the column/row names of x.
Andrej-Nikolai Spiess
## Speed comparison on a large matrix:
## rowVarsC is much faster than apply().
MAT <- matrix(rnorm(10 * 100000), ncol = 10)
system.time(RES1 <- apply(MAT, 1, var))
system.time(RES2 <- rowVarsC(MAT))
all.equal(RES1, RES2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.