mardi 19 janvier 2021

Select dates based on conditions in R

I am aiming to create a column based on conditions from 2 Date columns. The dataset looks like this:

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

The new column that I want to create is called "hospitalized_due_to_covid". And it is based on being hospitalized ("date_of_hospitalization") between 1 week before the test ("date_of_covid_test") & 1 month after the test.

If there is an NA, then the outcome would be a FALSE.

The outcome of the example that I posted here would be:

hospitalized_due_to_covid = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)

How could I code this?

Thank you very much in advance!! :)

Aucun commentaire:

Enregistrer un commentaire