I have a dataset that I’m trying to chunk up into "events" based on a condition. I want to create a consecutive group number (ID) which increases each time the condition is met.
Some kinds of records indicate that a new event has started, while other kinds of records represent no change / staying the course.
For example, in this dataset whenever ‘Action’ is "Left" or "Right", a new event has started and ‘Id’ should be incremented by 1:
| Id | Action |
|-----+---------|
| 1 | Left |
| 2 | Forward |
| 3 | Forward |
| 4 | Right |
| 5 | Forward |
| 6 | Left |
| ... | ... |
The resulting table I want would look like:
| Id | Action | GroupId |
|-----+---------+---------|
| 1 | Left | 1 |
| 2 | Forward | 1 |
| 3 | Forward | 1 |
| 4 | Right | 2 |
| 5 | Forward | 2 |
| 6 | Left | 3 |
| ... | ... | ... |
In something like python I might do this with a counter and a for loop (pseudo-ish code):
GroupID = 1
for row in data:
if Action == "Left" OR Action == "Right":
GroupID = GroupID + 1
else:
GroupID = GroupID
I feel like this should be a really simple one-liner, but my brain is broken right now and I’m having a hard time conceptualizing this.
GroupId = cumsum(Action %in% c("Left", "Right"))