[R] sequential sum of a vector...
Gustaf Rydevik
gustaf.rydevik at gmail.com
Wed Jul 23 17:35:48 CEST 2008
On Wed, Jul 23, 2008 at 4:03 PM, 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
>
>
How about
x<-1:80
filter(x,rep(1,8),"convolution",sides=1)[seq(8,length(x),by=8)]
##or
cumsum(x)[seq(8,length(x),by=8)]
?
/Gustaf
--
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik
More information about the R-help
mailing list