[R] accumulate() function in R?

gildororonar at mail-on.us gildororonar at mail-on.us
Sun Sep 15 04:36:23 CEST 2013


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

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!



More information about the R-help mailing list