日期比较 R

Date comparison R

我在 R 中进行日期比较,输出 class 很尴尬,因为我期待一个日期对象,但我收到一个数字。你能解释为什么吗?这背后的逻辑是什么?

library(data.table)
library(lubridate)

dt <- data.table(date_hist = ymd(seq.Date(as.Date("2018-01-01"), by = "month", length.out = 24)))
dt[,date_aux := ifelse(date_hist>max(date_hist),ymd(as.Date(max(date_hist),format = "%Y-%m-%d")),ymd(as.Date(date_hist,format = "%Y-%m-%d")))]

    date_hist date_aux
 1: 2018-01-01    17532
 2: 2018-02-01    17563
 3: 2018-03-01    17591
 4: 2018-04-01    17622
 5: 2018-05-01    17652
 6: 2018-06-01    17683
 7: 2018-07-01    17713
 8: 2018-08-01    17744
 9: 2018-09-01    17775
10: 2018-10-01    17805
11: 2018-11-01    17836
12: 2018-12-01    17866
13: 2019-01-01    17897
14: 2019-02-01    17928
15: 2019-03-01    17956
16: 2019-04-01    17987
17: 2019-05-01    18017
18: 2019-06-01    18048
19: 2019-07-01    18078
20: 2019-08-01    18109
21: 2019-09-01    18140
22: 2019-10-01    18170
23: 2019-11-01    18201
24: 2019-12-01    18231
     date_hist date_aux

sapply(dt,class)
date_hist  date_aux 
   "Date" "numeric"

而不是 ifelse 使用 fifelse

library(data.table)
dt[, date_aux := fifelse(date_hist>max(date_hist),ymd(as.Date(max(date_hist),
     format = "%Y-%m-%d")),ymd(as.Date(date_hist,format = "%Y-%m-%d")))]

str(dt)
#Classes ‘data.table’ and 'data.frame': 24 obs. of  2 variables:
# $ date_hist: Date, format: "2018-01-01" "2018-02-01" "2018-03-01" "2018-04-01" ...
# $ date_aux : Date, format: "2018-01-01" "2018-02-01" "2018-03-01" "2018-04-01" ...

ifelse,日期被转换成它的存储模式,即数字

如果我们检查源代码,就会发现问题是在最后几行赋值处造成的

...
ans <- test
len <- length(ans)
ypos <- which(test)
npos <- which(!test)
if (length(ypos) > 0L) 
    ans[ypos] <- rep(yes, length.out = len)[ypos]
if (length(npos) > 0L) 
    ans[npos] <- rep(no, length.out = len)[npos]
ans
...

举个简单的例子

v1 <- Sys.Date() -  1:5
v2 <- Sys.Date() + 1:5
ans <- v1 > Sys.Date() - 2 # logical vector
ypos <- which(ans)
npos <- which(!ans)
ans[ypos] <- rep(v2, length.out = length(ans))[ypos] Datess
ans
#[1] 18335     0     0     0     0

在逻辑向量上分配 Date class 强制日期转换为 numeric