This is similar to the previous post for the total amount that is reset based on the value in another column, except that I want to limit the amount so that it is also reset when it reaches the maximum value. For example, if the maximum value is 3:
> data.frame(x=rep(1,10),
+ y=c(0,0,1,0,0,0,0,1,0,0),
+ cum_sum_mod=c(1, 2, 1, 2, 3, 1, 2, 1, 2, 3))
x y cum_sum_mod
1 1 0 1
2 1 0 2
3 1 1 1
4 1 0 2
5 1 0 3
6 1 0 1
7 1 0 2
8 1 1 1
9 1 0 2
10 1 0 3
cum_sum_mod
sums column x until it reaches its maximum value (3), or the value in column y is 1. I want to avoid using a loop.
Vlad source
share