Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: retrieving individuals with given condition in the same date

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We could do

library(dplyr)
df %>%
   group_by(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  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading