I have a data frame that looks something like this:

| x | y | z |
| --- | --- | --- |
| 23 | 1 | 1 |
| 23 | 4 | 2 |
| 23 | 56 | 1 |
| 23 | 59 | 2 |
| 15 | 89 | 1 |
| 15 | 12 | 1 |
| 15 | 15 | 2 |
| 17 | 18 | 1 |
| 17 | 21 | 2 |
| 78 | 11 | 1 |
| 78 | 38 | 1 |
| 78 | 41 | 2 |

Now this data has certain pattern on column y and column z.
I want to get all the data where in column z we have a row wise pair of 2 followed by 1 for a given value in x. Simply put, we need to remove all rows that have 1 in column z but that 1 is not followed by 2 in next row.

The final output should look like this:

| x | y | z |
| --- | --- | --- |
| 23 | 1 | 1 |
| 23 | 4 | 2 |
| 23 | 56 | 1 |
| 23 | 59 | 2 |
| 15 | 12 | 1 |
| 15 | 15 | 2 |
| 17 | 18 | 1 |
| 17 | 21 | 2 |
| 78 | 38 | 1 |
| 78 | 41 | 2 |