R缺少日期计算

时间:2017-05-17 02:29:58

标签: r date data.table data-manipulation

我有data

Purshased_Date  value     Refund_Date   MONTH  YEAR   MDY         INTNX
2015-08-14      0         2015-08-14    8      2015   2015-08-01  2015-08-31
2015-08-14      96        NA            8      2015   2015-08-01  2015-08-31
2015-08-14      127       NA            8      2015   2015-08-01  2015-08-31

我想通过计算上面的数据来创建一个新列 代码是:

library(data.table)
data[, NewCol := value * pmax(0, difftime(pmin(Refund_Date, INTNX), pmax(Purshased_Date, MDY-1), units = 'day')) / as.numeric(difftime(INTNX, MDY-1, units = 'day'))]

但是,在pmin(Refund_Date1, INTNX) = NA时,R会考虑Refund_Date = NA 我不想使用NA 我想要的是Refund_Date = NApmin(Refund_Date, INTNX) = INTNX 我想要的结果如下:

Purshased_Date  value     Refund_Date   MONTH  YEAR   MDY         INTNX         NewCol
2015-08-14      0         2015-08-14    8      2015   2015-08-01  2015-08-31    0
2015-08-14      96        NA            8      2015   2015-08-01  2015-08-31    52.64516129
2015-08-14      127       NA            8      2015   2015-08-01  2015-08-31    69.64516129

我该怎么办?有什么建议吗?

0 个答案:

没有答案