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}}
Dear Shubha, Try this: x=1:80 tapply(x,rep(1:10,each=8),sum) 1 2 3 4 5 6 7 8 9 10 36 100 164 228 292 356 420 484 548 612 HTH, Jorge On Wed, Jul 23, 2008 at 10:03 AM, Shubha Vishwanath Karanth < shubhak@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@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. >[[alternative HTML version deleted]]
try this colSums(matrix(x,8)) regards, PF +------------------------------------------------- | Patrizio Frederic | Research associate in Statistics, | Department of Economics, | University of Modena and Reggio Emilia, | Via Berengario 51, | 41100 Modena, Italy | | tel: +39 059 205 6727 | fax: +39 059 205 6947 | mail: patrizio.frederic at unimore.it +------------------------------------------------- 2008/7/23 Shubha Vishwanath Karanth <shubhak at ambaresearch.com>:> 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. >
Many Thanks Jorge... That was one more way...Is it possible if I can do this without using rep(1:10,each=8) or the grouping....because I feel the number 8 here is fixed... If there is some technique of tracking the position of first 8, then next 8... don't know whether I am clear in conveying... Thanks, shubha ________________________________ From: Jorge Ivan Velez [mailto:jorgeivanvelez@gmail.com] Sent: Wednesday, July 23, 2008 8:59 PM To: Shubha Vishwanath Karanth Cc: r-help@stat.math.ethz.ch Subject: Re: [R] sequential sum of a vector... Dear Shubha, Try this: x=1:80 tapply(x,rep(1:10,each=8),sum) 1 2 3 4 5 6 7 8 9 10 36 100 164 228 292 356 420 484 548 612 HTH, Jorge On Wed, Jul 23, 2008 at 10:03 AM, Shubha Vishwanath Karanth <shubhak@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@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. This e-mail may contain confidential and/or privileged i...{{dropped:13}}
At 7:33 PM +0530 7/23/08, Shubha Vishwanath Karanth 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 >colSums(matrix(x,nrow=8)) Bill -- William Revelle http://personality-project.org/revelle.html Professor http://personality-project.org/personality.html Department of Psychology http://www.wcas.northwestern.edu/psych/ Northwestern University http://www.northwestern.edu/ Attend ISSID/ARP:2009 http://issid.org/issid.2009/
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
on 07/23/2008 09:03 AM Shubha Vishwanath Karanth 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, Shubhax <- 1:80 > colSums(matrix(x, ncol = 10)) [1] 36 100 164 228 292 356 420 484 548 612 If the original vector 'x' can be coerced to a rectangular matrix, you can create the matrix such that each column is a group: > matrix(x, ncol = 10) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 9 17 25 33 41 49 57 65 73 [2,] 2 10 18 26 34 42 50 58 66 74 [3,] 3 11 19 27 35 43 51 59 67 75 [4,] 4 12 20 28 36 44 52 60 68 76 [5,] 5 13 21 29 37 45 53 61 69 77 [6,] 6 14 22 30 38 46 54 62 70 78 [7,] 7 15 23 31 39 47 55 63 71 79 [8,] 8 16 24 32 40 48 56 64 72 80 Then just get the sums of each column. Note that by default, the matrix is formed by columns. This can be adjusted using the 'byrow' argument to matrix(). See ?matrix HTH, Marc Schwartz
> 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}}
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