[R] sequential sum of a vector...

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Wed Jul 23 17:37:40 CEST 2008


> x=1:80
> 
> I want to sum up first 8 elements of x, then again next 8 elements of x,
> then again another 8 elements..... So, my new vector should look like: 
> 
> c(36,100,164,228,292,356,420,484,548,612)
> 
> I used:
> 
> aggregate(x,list(rep(1:10,each=8)),sum)[-1]
> 
> or
> 
> rowsum(x,group=rep(1:10,each=8))
> 
> But without grouping, can I achieve the required? Any other ways of
> doing this?

This works:
colSums(matrix(x, nrow=8))

I'm not sure if you benefit over using the methods you described though.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list