Lili
Lili

Reputation: 587

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!

Upvotes: 0

Views: 406

Answers (1)

Leonardo
Leonardo

Reputation: 2485

If you are sure your columns are in date format you can try this:

library(dplyr)

df <- df %>% 
  mutate(
    condition = ifelse(date_of_covid_test < date_of_death, "yes", "no")
  )

where df is your dataframe and date_of_covid_test and date_of_death are the column names of the dataframe.

It works, but make sure the dates behave correctly!

Upvotes: 2

Related Questions