I have the following data
df = id Date Medication
1 2020-02-10 A
1 2020-02-10 B
1 2020-03-10 C
2 2020-05-30 A
2 2020-05-10 B
2 2020-05-10 C
3 2020-07-10 A
3 2020-07-10 B
3 2020-07-10 C
4 2020-09-01 A
4 2020-09-10 B
4 2020-09-10 C
I would like to extract those unique individuals with medication A and B on the same date. In this example, would be only id 1 (A and B given on the 2020-02-10) and 3 (A and B given on the 2020-05-10).
Thanks in advance
CodePudding user response:
library(dplyr)
df %>%
group_by(id) %>%
filter(Date[Medication == "A"] == Date[Medication == "B"])
CodePudding user response:
We could do
library(dplyr)
df %>%
group_by(id, Date) %>%
filter(all(c("A", "B") %in% Medication), Medication %in% c("A", "B")) %>%
ungroup
-output
# A tibble: 4 × 3
id Date Medication
<int> <chr> <chr>
1 1 2020-02-10 A
2 1 2020-02-10 B
3 3 2020-07-10 A
4 3 2020-07-10 B