sequential sum of a vector...

View: New views
10 Messages — Rating Filter:   Alert me  

sequential sum of a vector...

by Shubha Vishwanath Karanth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by Jorge Ivan Velez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by Patrizio Frederic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Parent Message unknown Re: sequential sum of a vector...

by Shubha Vishwanath Karanth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...]
Sent: Wednesday, July 23, 2008 8:59 PM
To: Shubha Vishwanath Karanth
Cc: r-help@...
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@...> 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.



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

by William Revelle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by Gustaf Rydevik :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by Marc Schwartz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Parent Message unknown Re: sequential sum of a vector...

by Richard Cotton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

______________________________________________
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.
Regards,
Richie.

Mathematical Sciences Unit
HSL

Re: sequential sum of a vector...

by Henrique Dallazuanna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by denise xifaras :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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


     
        [[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.
LightInTheBox - Buy quality products at wholesale price