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,]
Here's a solution using rowSums()
rowSums()
sapply( list(1:2,3:4) , function(i) rowSums(testM[,i]) )
if the number of columns should be arbitrary, it gets more complicated:
li <- split( 1:ncol(testM) , rep(1:(ncol(testM)/2), times=1 , each=2)) sapply( li , function(i) rowSums(testM[,i]) )