Thank you for your reply, I tried both your solutions and have formatted the data better now!
Using the dplyr code you suggested, the original:
ID Time data Pregnancy
1 1 0 1 yes
2 1 1 0 No
3 2 3 1 No
4 3 0 0 No
5 3 1 0 Yes
6 3 4 1 No
turned into:
# A tibble: 3 × 4
# Groups: ID [3]
ID Time data Pregnancy
<chr> <int> <chr> <chr>
1 1 1 0 No
2 2 3 1 No
3 3 0 0 No
So some worked correctly, whereas the row 1 should have also been removed, since that ID was pregnant at Time 0 and it has kept in the row for this ID (1) at Time 1 which is after the pregnancy. But the others seemed to have worked.
Also, the second data.table package gave me this error:
dt[Pregnancy == "No", .(Time = min(Time)), .(ID, data, Pregnancy)][, .(ID, Time, data, Pregnancy)][]
Error in .(Time = min(Time)) : could not find function "."