[Solved] Create sequence variable that restarts based on value of another column grouped by ID

Aepkr Asks: Create sequence variable that restarts based on value of another column grouped by ID
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:

Code:
# 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.

Code:
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!

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.