Re: [R] Extract vector elements until cumsum = x

2010-01-04 Thread Dieter Menne
Dgnn wrote: 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

Re: [R] Extract vector elements until cumsum = x

2010-01-04 Thread Gabor Grothendieck
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 sharkbrain...@gmail.com wrote: Hi All, I have a vector n, and for each n[i] I want to extract n[i], n[i+1],

Re: [R] Extract vector elements until cumsum = x

2010-01-04 Thread Dgnn
Thanks a lot, everybody! I've been working with these data for a while, and didn't realize that some of my assumptions lead me to leave out some important info. Specifically, that the vector contains time values of intervals between adjacent events, and so are always positive. Thanks again for

Re: [R] Extract vector elements until cumsum = x

2010-01-04 Thread Gabor Grothendieck
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 ggrothendi...@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,

[R] Extract vector elements until cumsum = x

2010-01-03 Thread Dgnn
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