I have a data.frame
like this:
> numeric_value <- c(10, 11, 21, 25, 15, 29, 30, 35, 9, 20, 21, 40, 22)
> threshold_reached <- c(F, F, T, T, F, T, T, T, F, T, T ,T, T)
> dat <- data.frame(numeric_value, threshold_reached)
I want a new variable for the maximum streak length of each streak (a streak being a TRUE
value of threshold_reached
), like this:
> max_streak_length <- c(0, 0, 2, 2, 0, 3, 3, 3, 0, 4, 4, 4, 4)
> data.frame(numeric_value, threshold_reached, max_streak_length)
numeric_value threshold_reached max_streak_length
1 10 FALSE 0
2 11 FALSE 0
3 21 TRUE 2
4 25 TRUE 2
5 15 FALSE 0
6 29 TRUE 3
7 30 TRUE 3
8 35 TRUE 3
9 9 FALSE 0
10 20 TRUE 4
11 21 TRUE 4
12 40 TRUE 4
13 22 TRUE 4
There are a few similar questions like this one and this one, which use the runner
package or rle
package. But I haven't found one that answers this specific problem, and I can't see a solution myself.
Preferably, I would like an answer using dplyr::mutate
but this isn't essential.
Thanks!