[R] sequential sum of a vector...
Henrique Dallazuanna
wwwhsd at gmail.com
Wed Jul 23 17:47:45 CEST 2008
Maybe:
tapply(x, gl(10, 8), sum)
or
unlist(lapply(split(x, gl(10, 8)), sum))
On Wed, Jul 23, 2008 at 11:03 AM, Shubha Vishwanath Karanth
<shubhak at ambaresearch.com> wrote:
> Hi R,
>
>
>
> Let,
>
>
>
> 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?
>
>
>
> Thanks, Shubha
>
>
>
> This e-mail may contain confidential and/or privileged i...{{dropped:13}}
>
> ______________________________________________
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list