用天而不是秒计算日期

date calculation with days rather than seconds

我有 2 列:

date1           date2
2015-01-01      2015-01-31
2015-01-02      2015-01-31
2015-01-03      2015-01-31
2015-10-27      2015-08-31    

我想根据 date1date2 创建新列 diff

data[,diff := pmax(0, date2 - date1)]

我想要的结果是这样的:

date1           date2          diff
2015-01-01      2015-01-31     30
2015-01-02      2015-01-31     29
2015-01-03      2015-01-31     28
2015-10-27      2015-08-31     -57

但是,我得到了这个:

date1           date2          diff
2015-01-01      2015-01-31     2592000
2015-01-02      2015-01-31     2505600
2015-01-03      2015-01-31     2419200
2015-10-27      2015-08-31     0

日期计算使用。但是,我想要
有帮助吗?

假设我们需要根据max的用法将负值改为0,那么将max替换为pmax

library(data.table)
setDT(data)[, diff := pmax(0, date2-date1)]
data
#        date1      date2 diff
#1: 2015-01-01 2015-01-31   30
#2: 2015-01-02 2015-01-31   29
#3: 2015-01-03 2015-01-31   28
#4: 2015-10-27 2015-08-31    0

如果我们遵循预期的输出

setDT(data)[, diff  := as.integer(date2-date1)]
data
#        date1      date2 diff
#1: 2015-01-01 2015-01-31   30
#2: 2015-01-02 2015-01-31   29
#3: 2015-01-03 2015-01-31   28
#4: 2015-10-27 2015-08-31  -57

更新

根据得到的输出 OP,我们认为它可能是基于原始数据集,一个选项是 difftime 其中 unit 参数强制输出在 'day'.

setDT(data)[, diff := pmax(0, difftime(date2,date1, units = 'day'))]

第二种情况

setDT(data)[, diff := as.integer(difftime(date2,date1, units = 'day'))]

OP 可能由于日期差异较小而获得了输出,这将其更改为 sec 而不是 day

更新2

OP 确认 'date' 列是 POSIXct。因此,我们也可以将其转换为 Date 并获得与 -

的差异
data[, diff := as.integer(as.Date(date2) - as.Date(date1))]

数据

data <- structure(list(date1 = structure(c(16436, 16437, 16438, 16735
), class = "Date"), date2 = structure(c(16466, 16466, 16466, 
16678), class = "Date")), .Names = c("date1", "date2"),
 row.names = c(NA, -4L), class = "data.frame")

差异让你秒,因为你的数据中可能有代表 times 而不是 dates 的对象。

如果您的数据真的只处理日期,即整日,请使用提供的 Date class。示例:

data = data.frame(
   date1=as.Date(c("2015-01-01","2015-01-02","2015-10-27")),
   date2 = as.Date(c("2015-01-31","2015-01-31","2015-08-31")))

然后:

> data$diff = data$date2 - data$date1
> data
       date1      date2     diff
1 2015-01-01 2015-01-31  30 days
2 2015-01-02 2015-01-31  29 days
3 2015-10-27 2015-08-31 -57 days
>