[R] Sum the cell of a vector

From: Alaios <alaios_at_yahoo.com>
Date: Tue, 03 May 2011 08:32:18 -0700 (PDT)


Dear all,
I would like to know what is the most time efficient way to calculate the following in a huge vector.

Let's say that I have the vector

1,2,3,4,5,6
and I want to return a vector of the same length which every cell containing the sum of the previous cells like this

1, 1+2, 1+2+3, 1+2+3+4, 1+2+3+4+5, 1+2+3+4+5+6

What do you think is the most effective way to do that (not having to do the previous sum). Of course I can do this with a for loop but I believe that might be a more efficient way in R.

I would like to thank you in advance for your help

Best Regards
Alex



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. Received on Thu 05 May 2011 - 06:25:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 May 2011 - 07:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive