vendredi 2 avril 2021

Difference between two dates as an if else statement in r

I have the following problem:

I try to build a new variable (FU for follow-up) with difference in two dates: The date of diagnosis and the date of death. But if the person is still alive the last date would be the last follow-up date.

I tried this:

df$FU = ifelse((df$deathdate < 1), 
           difftime(strptime("deathdate", format = "%Y-%m-%d"),
                                    strptime("diagnosisdate", format = "%Y-%m-%d"),units="days"), 
           difftime(strptime("lastfollowup", format = "%Y-%m-%d"),
                    strptime("diagnosisdate", format = "%Y-%m-%d"),units="days"))    

freq(df$FU)

Obviously I just get NAs. Does anyone knows how I can manage this? I need FU in days.

Regards Jessica

Aucun commentaire:

Enregistrer un commentaire