JasonAizkalns JasonAizkalns - 5 days ago 6
R Question

Sessionize a sequence of numbers into groups that reset once a cumulative threshold is met

Consider this sequence, which we can think of as "time between events"

x <- c(5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2)


I would like to group these into buckets of 30, but buckets that reset. Desired outcome:

output <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)


This is because, when we get to a cumulative 30, we "reset" and begin counting again. So,
5 + 40 > 30
, we drop down to zero and begin cumulative adding until we reach 30...(
3 + 6 + 0 ...
), which happens at when we reach
x[10] == 18
.

Answer

One option is to use Reduce() to calculate the cumulative sum where you can set the sum to be zero, when it exceeds some threshold:

cumsum(Reduce(function(x, y) if(x < 30) x + y else 0, x, acc = T) >= 30)
# [1] 0 1 1 1 1 1 1 1 1 2 2 2 2 2