[R] Cumulative sum of vector

Gabor Csardi csardi at rmki.kfki.hu
Sun Jan 6 15:50:24 CET 2008


Keith, are you looking for 'cumsum' ?

Gabor

On Sat, Jan 05, 2008 at 08:32:41AM -0600, Keith Jones wrote:
> Hi,
> 
> Maybe I have not been looking in the right spot, but, I have not been 
> able to fine a command to automatically calculate the running 
> cumulative sum of a vector.  Is there such a command?
> 
> Example of current code:
> >  eig$values
> [1] 678.365651   6.769697   2.853783
> >  prop<-eig$values/sum(eig$values)
> >  prop
> [1] 0.986012163 0.009839832 0.004148005
> >  cum<-c(prop[1],sum(prop[1:2]),sum(prop[1:3]))
> >  cum
> [1] 0.9860122 0.9958520 1.0000000
> 
> This works, but, if the length of the vector changes I have to 
> manually change the code.
> 
> Thanks,
> 
> Keith Jones
> 
> ______________________________________________
> 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.

-- 
Csardi Gabor <csardi at rmki.kfki.hu>    MTA RMKI, ELTE TTK




More information about the R-help mailing list