I have a dateframe like this one with variables corresponding to death on inclusion and variables corresponding to death on follow-up visits:
Id | death_inclusion | date_death_inc | follow-up | death_followup | date_death_followup |
---|---|---|---|---|---|
1 | yes | 2004-08-15 | |||
2 | no | 1 | no | ||
2 | no | 2 | no | ||
2 | no | 3 | yes | 2011-10-16 | |
3 | no | 1 | no | ||
3 | no | 2 | no | ||
4 | no | 1 | yes | 2013-05-03 |
I would like to obtain a dataframe like the one below to have the vital status of the patient whether he died at inclusion or at one of the follow-up visits. I was thinking of using functions such as “group_by” and if/ then/else
Id | death | date_death |
---|---|---|
1 | yes | 2004-08-15 |
2 | yes | 2011-10-16 |
3 | no | |
4 | yes | 2013-05-03 |
Here's a reproducible code
df <- data.frame(ID = c("1", "2", "2","2","3","3","4"), death_inclusion = c("Yes", "No", "No","No","No","No","No"), date_death_inc = c("2004-08-15", "", "","","","",""), follow_up = c("", "1", "2","3","1","2","1"), death_followup = c("", "No", "No","Yes","No","No","Yes"), date_death_followup = c("", "", "","2011-10-16","","","2013-05-03"))df$date_death_inc<-as.Date(df$date_death_inc,format="%Y-%m-%d")df$date_death_followup<-as.Date(df$date_death_followup,format="%Y-%m-%d")
Thank you for your help