Probably this would be easy. I have a Matrix:
testM <- matrix(1:40, ncol = 4, byrow = FALSE) testM [,1] [,2] [,3] [,4] [1,] 1 11 21 31 [2,]
We can do a matrix multiplication:
M <- matrix(c(1,1,0,0, 0,0,1,1), 4, 2) testM %*% M
another solution with tapply():
tapply()
g <- gl(ncol(testM)/2, 2) t(apply(testM, 1, FUN=tapply, INDEX=g, sum))