I have some data that I am trying to group by consecutive values in R. This solution is similar to what I am looking for, however my data is structured like this:
line_num |
---|
1 |
2 |
3 |
1 |
2 |
1 |
2 |
3 |
4 |
What I want to do is group each time the number returns to 1 such that I get groups like this:
line_num | group_num) |
---|---|
1 | 1 |
2 | 1 |
3 | 1 |
1 | 2 |
2 | 2 |
1 | 3 |
2 | 3 |
3 | 3 |
4 | 3 |
Any ideas on the best way to accomplish this using dplyr or base R? Thanks!