I need to apply a rolling function that sums the rows of every two columns, so the rows of columns 1&2 will be summed, 3&4, etc.
m<-matrix(c(1,2,3,4,5,3,4,5,6,2,4,6,6,7,3,2,4,4,5,7),nrow=2,byrow=T)
I have looked at many functions including apply, rollapply, aggregate, etc. but cant seem to find one that roll sums the rows of specified columns.
I am more than capable of writing the code out the long way however am looking for an efficient solution which most likely involves a function.
sum1<-(m[,1]+m[,2])
sum2<-(m[,3]+m[,4])
sum3<-(m[,5]+m[,6])
sum4<-(m[,7]+m[,8])
sum5<-(m[,9]+m[,10])
cbind(sum1,sum2,sum3,sum4,sum5)
Thanks!
You can use this approach: