1

我想比较两个“日期字符串”列,例如:

df$inpatient.death = (df$date.of.death==df$date.of.discharge)

但是:NULL值的出现似乎使我无法格式化as.Date,并且不同的格式无法使用 as.character(..)==as.character(..)。最好的创作方式是什么

                                                    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"))

这样做的最佳方法是什么?

4

1 回答 1

1
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
于 2013-05-26T18:42:47.707 回答