如何使用R中data.table中组内的最后一行进行一些计算?

时间:2015-08-19 16:53:49

标签: r data.table

我有这个data.table:

样品:

id cond date
1  A1   2012-11-19
1  A1   2013-05-09
1  A2   2014-09-05
2  B1   2015-03-05
2  B1   2015-07-06
3  A1   2015-02-05
4  B1   2012-09-26
4  B1   2015-02-05
5  B1   2012-09-26

我想计算从今天开始的每天过期的过期日期' id'和' cond',所以我试图获得每个组中最后一个日期和sys.date之间的天数差异。期望的输出是;

id cond date        overdue
1  A1   2012-11-19  NA
1  A1   2013-05-09  832 
1  A2   2014-09-05  348
2  B1   2015-03-05  NA 
2  B1   2015-07-06  44
3  A1   2015-02-05  195
4  B1   2012-09-26  NA 
4  B1   2015-02-05  195
5  B1   2012-09-26  1057

我尝试通过以下代码实现此目的:

sample <- sample[ , overdue := Sys.Date() - date[.N], by = c('id','cond')]

但是我得到了以下输出,其值是回收:

id cond date        overdue
1  A1   2012-11-19  832
1  A1   2013-05-09  832 
1  A2   2014-09-05  348
2  B1   2015-03-05  44 
2  B1   2015-07-06  44
3  A1   2015-02-05  195
4  B1   2012-09-26  195 
4  B1   2015-02-05  195
5  B1   2012-09-26  1057

我不确定,如何限制我的代码只是为最后一行进行计算而不是回收。我相信会有办法做到这一点,感谢帮助。

2 个答案:

答案 0 :(得分:6)

您可以制作一个过期值及其所属行的表格:

bycols    = c("id","cond")
newcolDT2 = DT[, Sys.Date() - date[.N], by = bycols]

DT[newcolDT2, overdue := V1, on = bycols, mult = "last"]
#    id cond       date   overdue
# 1:  1   A1 2012-11-19   NA days
# 2:  1   A1 2013-05-09  832 days
# 3:  1   A2 2014-09-05  348 days
# 4:  2   B1 2015-03-05   NA days
# 5:  2   B1 2015-07-06   44 days
# 6:  3   A1 2015-02-05  195 days
# 7:  4   B1 2012-09-26   NA days
# 8:  4   B1 2015-02-05  195 days
# 9:  5   B1 2012-09-26 1057 days

这是(可以说是丑陋的)单行版本:

DT[J(unique(DT[, bycols, with=FALSE])), 
  overdue := Sys.Date() - date, on = bycols, mult = "last"]

数据:

DT <- data.table(read.table(header=TRUE,text="id cond date
1  A1   2012-11-19
1  A1   2013-05-09
1  A2   2014-09-05
2  B1   2015-03-05
2  B1   2015-07-06
3  A1   2015-02-05
4  B1   2012-09-26
4  B1   2015-02-05
5  B1   2012-09-26"))[, date := as.IDate(date)]

# anyone know how to do this with fread()?

答案 1 :(得分:3)

首先,提取您感兴趣的行,然后分配值:

rows = DT[, .I[.N], by = .(id, cond)]$V1
DT[rows, overdue := Sys.Date() - date]

DT
#   id cond       date   overdue
#1:  1   A1 2012-11-19   NA days
#2:  1   A1 2013-05-09  832 days
#3:  1   A2 2014-09-05  348 days
#4:  2   B1 2015-03-05   NA days
#5:  2   B1 2015-07-06   44 days
#6:  3   A1 2015-02-05  195 days
#7:  4   B1 2012-09-26   NA days
#8:  4   B1 2015-02-05  195 days
#9:  5   B1 2012-09-26 1057 days