Hi,
I am trying to remove all rows from a dataframe based on a value in a column, but for each unique ID as shown below.
ID | Time | Data | Pregnancy
1 | 0 | 1 | Yes
1 | 1 | 0 | No
2 | 3 | 1 | No
3 | 0 | 1 | No
3 | 1 | 0 | Yes
3 | 4 | 1 | No
so it would show in the end:
ID | Time | Data | Pregnancy
2 | 3 | 1 | No
3 | 0 | 1 | No
So I am trying to ask, if df$Pregnancy = "Yes" then remove all rows for that same ID where Time is after or equal to the Time at Pregnancy.
ID <- c("1", "1", "2", "3", "3", "3")
Time <- c(0L, 1L, 3L, 0L, 1L, 4L)
data <- c("1", "0", "1", "0", "0", "1")
Pregnancy <- c("yes", "No", "No", "No", "Yes", "No")
df <- data.frame(ID, Time, data, Pregnancy)
I am stuck as to the code I would require to do this, and any help would be greatly appreciated as still very new to R!