I want a variable such as desired_output
, based on a cumulative sum over cumsumover
, where the cumsum
function resets every time it reaches the next number in thresh
.
cumsumover <- c(1, 2, 7, 4, 2, 5)
thresh <- c(3, 7, 11)
desired_output <- c(3, 3 ,7 ,11 ,11 ,11) # same length as cumsumover
This question is similar, but I can't wrap my head around the code. dplyr / R cumulative sum with reset
Compared to similar questions my condition is specified in a vector of different length than the cumsumover
.
Any help would be greatly appreciated. Bonus if both a base R and a tidyverse approach is provided.