I have a data set that has start date and end date. Some of the end dates are missing. As you can see below, I have tried three different approaches and none of them is working.
startDay <- as.Date(c("2015-01-01","2015-03-01","2016-07-15","2016-08-02"), "%Y-%m-%d")
endDay <- as.Date(c("2018-01-01",NA,"2018-03-05",NA), "%Y-%m-%d")
id <- 1:4
dt <- data.frame(id, startDay, endDay)
dt
str(dt)
dt$caseDay <- as.Date("2018-07-20", "%Y-%m-%d")
str(dt)
dt
This one changes the class of my variable from date to numeric:
dt$EndDay1 <-
ifelse(is.na(dt$endDay), dt$caseDay, dt$endDay)
str(dt)
dt
This one generates an error message.
dt$EndDay2 <-as.Date(
ifelse(is.na(dt$endDay), dt$caseDay, dt$endDay), "%Y-%m-%d")
str(dt)
dt
If my research/understanding of related posts is correct, version 3 below should resolve the problem. However, this converted everything to missing values.
dt$EndDay3 <-as.Date(as.character(
ifelse(is.na(dt$endDay), dt$caseDay, dt$endDay)), "%Y-%m-%d")
str(dt)
dt
Any suggestion on how to solve this? Thanks