replacing values in R based on conditions in same and other variables

14 Views Asked by At

I am working on a large data set about vaccination events. among other variables we have patient ID, date of vaccination and vaccine type. some entries are redundant, for example where vaccine a is a combination vaccine and vaccine b is one of the components of vaccine a, there are entries for both vaccine a and vaccine b for the same patient and on the same date of vaccination. I am trying to find a solution where if both vaccine a and vaccine b are listed with both the ID variable and date of vaccination variable are the same, vaccine b would be changed to NULL.

DF1 <- data.frame(ID=c("P1","P1","P1","P2","P3","P3"),
        date_of_service=c("D1","D1","D2","D1","D1","D3"),
        vaccine=c("a","b","a","b","c","b"))


ID  Datev   vaccine
        

1 P1 D1 a 2 P1 D1 b 3 P1 D2 a 4 P2 D1 b 5 P3 D1 c 6 P3 D3 b

The desired out come should be

ID  Datev   vaccine
        

1 P1 D1 a 2 P1 D1 NULL 3 P1 D2 a 4 P2 D1 b 5 P3 D1 c 6 P3 D3 b

0

There are 0 best solutions below