[R] accumulate() function in R?

Berend Hasselman bhh at xs4all.nl
Sun Sep 15 08:38:11 CEST 2013


On 15-09-2013, at 04:36, gildororonar at mail-on.us wrote:

> I came from Python, newly learning R. is there something like accumulate() in R?
> 

Yes: cumsum

Berend

> Example:
> accumulate([1,2,3,4,5]) --> 1 3 6 10 15
> 
> Or perhaps I should show the problem. The problem I am trying to solve, is to select elements from X until it accumulate to 30. My solution is:
> 
>> X = c(1,3,4,5,8,15,35,62,78,99)
>> X[sapply(seq_len(length(X)), function(x) { sum(X[1:x])}) < 30]
> [1] 1 3 4 5 8
> 
> Is this already the shortest/canonical way to do it in R?
> 
> 
> -------------------------------------------------
> 
> VFEmail.net - http://www.vfemail.net
> $14.95 ONETIME Lifetime accounts with Privacy Features! 15GB disk! No bandwidth quotas!
> Commercial and Bulk Mail Options!
> 
> ______________________________________________
> 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.



More information about the R-help mailing list