Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

use group_by function with if/then/else condition

$
0
0

I have a dateframe like this one with variables corresponding to death on inclusion and variables corresponding to death on follow-up visits:

Iddeath_inclusiondate_death_incfollow-updeath_followupdate_death_followup
1yes2004-08-15
2no1no
2no2no
2no3yes2011-10-16
3no1no
3no2no
4no1yes2013-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

Iddeathdate_death
1yes2004-08-15
2yes2011-10-16
3no
4yes2013-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


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>