I want to create a cumulative count of adjacent repetitions in a column. For instance the desired output for repeat_n
in the code below should be c(1,2,3,1,2,1,2), but instead I am getting c(1,2,2,1,2,1,2). Perhaps because case_when()
is vectorized, case_when()
evaluates all values simultaneously rather sequentially evaluating the updated values. How should I avoid this problem?
library(dplyr)
tibble(x = c(1,1,1,0,0,1,1)) %>%
mutate(
repeat_n = 1,
repeat_n =
case_when(
x == lag(x) ~ lag(repeat_n) + 1,
TRUE ~ repeat_n
)
)