maybe ?cumsum z <- 1:10 cumsum( z ) z <- sort( z ) cumsum( z )[ cumsum( z ) < 30 ]
On Sat, Sep 14, 2013 at 10:36 PM, <gildororo...@mail-on.us> wrote: > 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! > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.