I have a dataset similar to this:
df <-
read.table(textConnection("ID Column1 Column2
A 0 1
A 1 0
A 1 0
A 1 0
A 0 1
A 1 0
A 0 1
A 0 0
A 1 0
A 1 0
B 0 1
B 1 0
C 0 1
C 0 0
C 1 0"), header=TRUE)
I am looking to do a group_by ID in dplyr that maintains records where Column2 = '1' and the record underneath it has Column1 = '1'. This may happen more than once per ID; all other records should be excluded. So the output from the above should be:
| ID | Column1 | Column2 |
|---|---|---|
| A | 0 | 1 |
| A | 1 | 0 |
| A | 0 | 1 |
| A | 1 | 0 |
| B | 0 | 1 |
| B | 1 | 0 |
Any help will be very much appreciated, thanks!