Reputation: 1085
I would like to compare two 'date string' columns like:
df$inpatient.death = (df$date.of.death==df$date.of.discharge)
BUT: the occurrence of NULL
values seems to prevent me from formatting as.Date
, and the different format from using as.character(..)==as.character(..).
What's the best way of creating
THIS IS THE AIM:
id date.of.death date.of.discharge [ inpatient.death ]
1 1 2012-01-01 00:00:00.000 2012-01-01 [ TRUE ]
2 2 NULL 2012-01-01 [ FALSE ]
3 3 2012-01-02 00:00:00.000 2012-01-01 [ FALSE ]
df <- data.frame(id=1:3, date.of.death=c("2012-01-01 00:00:00.000", "NULL", "2012-01-02 00:00:00.000"), date.of.discharge=c("2012-01-01", "2012-01-01", "2012-01-01"))
What's the best way of doing this?
Upvotes: 1
Views: 5126
Reputation: 470
df <- data.frame(id=1:3, date.of.death=c("2012-01-01 00:00:00.000", "NULL", "2012-01-02 00:00:00.000"),
date.of.discharge=c("2012-01-01", "2012-01-01", "2012-01-01"))
df$inpatient.death <- as.Date(df$date.of.death)==as.Date(df$date.of.discharge) # date.of.death is already in the standard format no need to specify
df$inpatient.death[is.na(df$inpatient.death)] <- F
> df
id date.of.death date.of.discharge inpatient.death
1 1 2012-01-01 00:00:00.000 2012-01-01 TRUE
2 2 NULL 2012-01-01 FALSE
3 3 2012-01-02 00:00:00.000 2012-01-01 FALSE
# you can also definy an helper function for this task
`==2` <- function(x,y){
res <- x==y
res[is.na(res)] <- F
res
}
df$inpatient.death <- `==2`(as.Date(df$date.of.death),as.Date(df$date.of.discharge))
> df
id date.of.death date.of.discharge inpatient.death
1 1 2012-01-01 00:00:00.000 2012-01-01 TRUE
2 2 NULL 2012-01-01 FALSE
3 3 2012-01-02 00:00:00.000 2012-01-01 FALSE
Upvotes: 1