I would like to create a sequence variable that resets when the value of another column exceeds 7. Additionally, I would like the sequencing to be grouped by ID and in order of date.
That is like column 3 in the example data frame below:
# ID Column_1 Column_2 Column_3
# 1 DATE 0 0
# 1 DATE 2 1
# 1 DATE 4 2
# 1 DATE 100 0
# 1 DATE 3 1
# 1 DATE 50 0
# 1 DATE 2 1
# 1 DATE 5 2
# 1 DATE 20 0
# 1 DATE 3 1
# 1 DATE 59 0
# 1 DATE 1 1
I have tried the following but it doesn't seem to work.
df <- df %>%
group_by(ID) %>%
arrange(DATE) %>%
mutate(Column_3 = ave(Column_2, cumsum(Column_2 >7)), .after =
Column2)
Thank you in advance for any help!