[R] Extract vector elements until cumsum <= x

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 5 03:38:47 CET 2010


Mark Leeds pointed out to me that the last line should have been:

Reduce(f, x, accumulate = TRUE)

On Mon, Jan 4, 2010 at 8:43 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Try this which uses 10 as the check value:
>
> f <- function(acc, x) if (acc + x > 10) x else acc + x
> x <- c(4, 6, 2, 7, 4, 1, 4, 8)
> Reduce(f, x)
>
>
> On Sun, Jan 3, 2010 at 10:02 PM, Dgnn <sharkbrainpdx at gmail.com> wrote:
>>
>> Hi All,
>>
>> I have a vector n, and for each n[i] I want to extract n[i], n[i+1],
>> n[i+2]..., until the cumulative sum of n[i] and subsequent elements exceeds
>> a CheckValue, whereupon I move to the next index and repeat.
>>
>> I am trying to find a Vectorized approach, and have seen similar posts where
>> filter{stat} and rollmean{zoo} were suggested, but, I haven't been able to
>> figure a way to use them for this problem.
>>
>> Best wishes to all, and thanks for the help!
>>
>> Dgnn
>>
>>
>> --
>> View this message in context: http://n4.nabble.com/Extract-vector-elements-until-cumsum-x-tp998023p998023.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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