How can I do a cumulative sum over a vector (like cumsum
), but bounded so that the summation never goes below a lower bound or above an upper bound?
The standard cumsum function would result in the following.
foo <- c(100, -200, 400, 200)
cumsum(foo)
# [1] 100 -100 300 500
I am looking for something as efficient as the base cumsum
function. I would expect the output to look like the following.
cumsum.bounded(foo, lower.bound = 0, upper.bound = 500)
# [1] 100 0 400 500
Thanks
cumsum
function, you have to implement it inC
. - Sven HohensteinRcpp
solution. - Richie Cotton