vendredi 15 janvier 2021

Create a column based on meeting a condition in 2 as.Date columns in R

In my df I have 2 columns:

"date of covid test" & "date of death"

Both columns have A LOT of NAs. And it can happen that someone passed away and never had a covid test or someone had a covid test but never passed away. Example:

df <- data.frame(PatientID = c("3454","345","5","345","567","79"),
                 date_of_covid_test = c(2020-04-02, NA, NA, 2020-11-03, NA, 2020-12-05),
                 date_of_death = c(2020-05-03, 2000-03-01, 2000-01-01, NA, NA, NA), stringsAsFactors = F)

I would like to create an extra column based on this sentence:

"If date of death happens AFTER date of covid test = yes, otherwise = no"

The expected output would be: that only patient 3454 would be TRUE and all the others would be FALSE.

Basically the point is to know whether the death happened after the covid test, that would be a TRUE. Alternatively would be a FALSE.

I hope this makes sense

What is the right way of coding this?

Thanks!

Aucun commentaire:

Enregistrer un commentaire