Compute Variance in each column between certain number of rows

I want to compute the variances for each column of a matrix, but that variance must be calculated every 7 rows, for example

9.8  4.5  0.9  7.8.....
5.4  9.8  1.2  3.5....
3.1  2.6  9.5  7.1.....
3.4  NA   1.1  1.5.....
7.9  5.9  3.4  2.6.....
4.5  5.1  7.4  NA.....
VAR  VAR  VAR  VAR

VAR is the variace of the column. After 7 rows in the same matrix I have to compute the variance again, removing the NA´s. The dimension of the matrix is 266x107.

I tried with the colVars from the boa package, but that command compute the variance for the entire column.

Upvotes: 0

Views: 2344

Answers (2)

David
David

Reputation: 216

Here is the data.table approach:

require(data.table)
# Create the data table
dt <- as.data.table(matrix(rnorm(266*107), 266, 107))
# For every 7 rows, calculate variance of each column, ignoring NAs
dt[, lapply(.SD, var, na.rm=T), by=gl(ceiling(266/7), 7, 266)]

Upvotes: 1

Julian Wittische
Julian Wittische

Reputation: 1237

aggregate() is a mighty function for this kind of tasks, no need for another package in this case:

lolzdf <- matrix(rnorm(266*107), 266, 107)
    n<-7 
    aggregate(lolzdf,list(rep(1:(nrow(lolzdf)%/%n+1),each=n,len=nrow(lolzdf))),var,na.rm=TRUE)[-1];

Upvotes: 0

Related Questions