[R] adding rows

Rui Barradas ruipbarradas at sapo.pt
Fri May 9 23:55:27 CEST 2014


Hello,

Try the following.

sapply(1:(30 - 2), function(i) sum(el[i:(i+2), ]))


but with number of rows instead of 30.

Hope this helps,

Rui Barradas

Em 09-05-2014 22:35, eliza botto escreveu:
> Dear useRs,
> I have a matrix, say "el" of 30 rows and 10 columns, as
> el<-matrix(sample(1:300),ncol=10)
> I want to sum up various sets of three rows of each column in the following manner
> sum(el[c(1,2,3),]) ##adding row number 1, 2 and 3 of each column
> sum(el[c(2,3,4),])##adding row number 2, 3 and 4 of each column
> sum(el[c(3,4,5),])##adding row number 3, 4 and 5 of each column
> sum(el[c(4,5,6),])
> sum(el[c(5,6,7),])
> sum(el[c(6,7,8),])
> sum(el[c(7,8,9),])
> sum(el[c(8,9,10),])
> sum(el[c(9,10,11),])
> ................
> ......
> so on
> ..............
> I know how to do it manually, but since my original matrix has 2000 rows, I therefore want to figure out a more conveinient way.
> Thankyou so very much in advance,
>
> Eliza 		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list