|
View:
New views
10 Messages
—
Rating Filter:
Alert me
|
|
|
sequential sum of a vector...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@... 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. |
|
|
Re: 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@...> 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@... 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]] ______________________________________________ R-help@... 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. |
|
|
Re: sequential sum of a vector...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@... +------------------------------------------------- 2008/7/23 Shubha Vishwanath Karanth <shubhak@...>: > 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@... 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. > ______________________________________________ R-help@... 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. |
|
|
|
|
|
Re: sequential sum of a vector...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/ ______________________________________________ R-help@... 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. |
|
|
Re: sequential sum of a vector...On Wed, Jul 23, 2008 at 4:03 PM, Shubha Vishwanath Karanth
<shubhak@...> 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 ______________________________________________ R-help@... 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. |
|
|
Re: sequential sum of a vector...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, Shubha x <- 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 ______________________________________________ R-help@... 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. |
|
|
|
|
|
Re: sequential sum of a vector...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@...> 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@... 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 ______________________________________________ R-help@... 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. |
|
|
Re: sequential sum of a vector...Dear Patrizio and all, Do you know a similar approach to this, so that *any* function is sequentially applied on a matrix? Particularly, instead of wanting to sum columns every 8 rows, how could we apply a linear regression to the columns of a matrix every 8 rows? ie, if we have a matrix say of 100 rows and 3 columns- say columns are price, production cost, desired profit. How can we apply lm(price~production cost +desired profit) where the data is taken every 10 lines from the matrix, ie we end up with 10 different regressions? Any help is welcome, Thank you very much Denise --- On Wed, 7/23/08, Patrizio Frederic <frederic.patrizio@...> wrote: From: Patrizio Frederic <frederic.patrizio@...> Subject: Re: [R] sequential sum of a vector... To: "Shubha Vishwanath Karanth" <shubhak@...> Cc: r-help@... Date: Wednesday, July 23, 2008, 8:30 AM 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@... +------------------------------------------------- 2008/7/23 Shubha Vishwanath Karanth <shubhak@...>: > 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@... mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@... 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]] ______________________________________________ R-help@... 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. |
| Free Forum Powered by Nabble | Forum Help |