Re: [R] sequential sum of a vector...

From: <>
Date: Wed, 23 Jul 2008 16:37:40 +0100

> 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.


Mathematical Sciences Unit

ATTENTION: This message contains privileged and confidential inform...{{dropped:20}} mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 23 Jul 2008 - 16:37:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Jul 2008 - 17:32:25 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive