Say I have
a <- c(0, 22, 0, 2, 0, 0, 20, 20, 20, 0, 0)
I want to make a cumulative amount in which I minus 5for each value in a, and then add the previous value.
However, I also have the condition that if ait gets smaller 0, I want cumsum to be 0, and if ait gets bigger 40, for cumsum to 40.
So I want to get
(0, 17, 12, 9, 4, 0, 15, 30, 40, 35, 30)
Can anyone help? I’ve tried it for hours now for several hours!
@Holger, this method does not always work. Therefore, if I add a few extra zeros, it will not have the right solution
a <- c(0, 22, 0, 2, 0, 0, 0, 0, 20, 20, 20, 0, 0)
gives
0 17 12 9 4 0 1 7 22 37 52 47 42