I would like to delete ID in any row that contains certain strings (A or D) within ID. Here is my data frame:
id time dx
1 1 C
1 2 B
2 1 A
2 2 C
2 3 B
3 1 D
I would like the following:
id time dx
1 1 C
1 2 B
Based on the earlier post regarding this (Delete rows containing specific strings in R), I tried d %>% filter(!grepl('A|D', dx)). However, it only deletes the rows that contain A or D, not the whole IDs. I'd appreciate any help!
##Update: All the below answers worked well for the above post. Thank you all! Note that for this post, I simplified my data frame, and later on, I realized that I actually needed R codes to delete the IDs with certain partial strings (e.g., A or B0) from the following data frame. I was able to achieve this by modifying the first r2evans' answer: d %>% group_by(id) %>% filter(!any(str_detect(dx, "A|B0"))) %>% ungroup(). I have included the note here in case someone needs it. I would appreciate any additional suggestions.
Data frame:
id time dx
1 1 C01
1 2 B1
2 1 A34
2 2 C01
2 3 B1
3 1 B01X
The results I wanted:
id time dx
1 1 C01
1 2 B1