Flag a row after conditions met in R
I have the following dataframe in R:
| id | date | time | cost | value |
|---|---|---|---|---|
| 1 | 2026-01-24 | 10:52:44 | 234 | 34 |
| 1 | 2026-01-24 | 10:54:44 | 236 | 68 |
| 1 | 2026-01-24 | 11:05:41 | 353 | 99 |
| 1 | 2026-01-25 | 11:52:44 | 5352 | 78 |
| 1 | 2026-01-25 | 11:58:14 | 143 | 99 |
| 2 | 2026-01-24 | 10:02:44 | 124 | 99 |
| 2 | 2026-01-24 | 10:22:44 | 636 | 99 |
| 3 | 2026-01-24 | 10:52:12 | 53 | 75 |
| 3 | 2026-01-25 | 10:52:44 | 436 | 99 |
| 3 | 2026-01-25 | 11:12:23 | 9473 | 13 |
For each id, I need to go through and tag each row after a value of say 99 here. I have it sorted in date/time order so it should be the next row. The desired output is something like this:
| id | date | time | cost | value | new |
|---|---|---|---|---|---|
| 1 | 2026-01-24 | 10:52:44 | 234 | 34 | 0 |
| 1 | 2026-01-24 | 10:54:44 | 236 | 68 | 0 |
| 1 | 2026-01-24 | 11:05:41 | 353 | 99 | 1 |
| 1 | 2026-01-25 | 11:52:44 | 5352 | 78 | 1 |
| 1 | 2026-01-25 | 11:58:14 | 143 | 99 | 1 |
| 2 | 2026-01-24 | 10:02:44 | 124 | 99 | 1 |
| 2 | 2026-01-24 | 10:22:44 | 636 | 99 | 1 |
| 3 | 2026-01-24 | 10:52:12 | 53 | 75 | 0 |
| 3 | 2026-01-25 | 10:52:44 | 436 | 99 | 1 |
| 3 | 2026-01-25 | 11:12:23 | 9473 | 13 | 1 |
Can anyone advise on the most efficient way to do this please? Thanks in advance!