根据另一个data.table的最后一次更改来更新data.table

时间:2014-10-14 22:36:55

标签: r join data.table

我目前有两个数据表如下所示:

dt1 <- data.table(urn=c("1","1","1","1","1","1","1","2","2","2","2","3","3","3","3","3","3","3","2"),
                  date=as.Date(c("2014-01-15","2014-02-15","2014-03-15","2014-04-15","2014-05-15","2014-06-15","2014-07-15",
                         "2014-04-15","2014-05-15","2014-06-15","2014-07-15",
                         "2014-04-15","2014-03-15","2014-05-15","2014-02-15","2014-06-14","2014-08-15","2014-07-15","2014-09-16")),
                  amount=c(20,20,15,15,15,20,25,
                           15,15,20,20,
                           30,30,30,30,25,25,25,20))

#dt1
#    urn       date amount
# 1:   1 2014-01-15     20
# 2:   1 2014-02-15     20
# 3:   1 2014-03-15     15
# 4:   1 2014-04-15     15
# 5:   1 2014-05-15     15
# 6:   1 2014-06-15     20
# 7:   1 2014-07-15     25
# 8:   2 2014-04-15     15
# 9:   2 2014-05-15     15
#10:   2 2014-06-15     20
#11:   2 2014-07-15     20
#12:   3 2014-04-15     30
#13:   3 2014-03-15     30
#14:   3 2014-05-15     30
#15:   3 2014-02-15     30
#16:   3 2014-06-14     25
#17:   3 2014-08-15     25
#18:   3 2014-07-15     25
#19:   2 2014-09-16     20

dt2 <- data.table(urn=c("1","2","3"), lastamount=c(25,20,25),lastchangedate=as.Date(c(NA,NA,NA)))

#dt2
#    urn lastamount lastchangedate
# 1:   1         25           <NA>
# 2:   2         20           <NA>
# 3:   3         25           <NA>

需要注意的几件重要事项,(1)骨灰盒总是没有订购,(2)日期总是没有正确订购,(3)金额可能会上升或下降(&) 4)日期可以跳过一个月,(5)日期不总是在一个月的同一天。

我想要达到的目的是更新dt2 lastchangedate以反映更改金额的最新日期(增加或减少)。

所以我希望dt2在处理后看起来如下所示。

#dt2
#    urn lastamount lastchangedate
# 1:   1         25     2014-07-15
# 2:   2         20     2014-06-15
# 3:   3         25     2014-06-14

这显然是一个示例数据集。为了给出一个数量级,我的真实dt1有3.5M记录,dt2有250K记录。

由于

更新: 由于我的真实dt2列的数量多于样本中显示的列数,因此我需要能够将它们保留在最终输出中。而不是覆盖dt2的当前实例,我想只是单独更新lastchangedate。以下是我使用的代码。

setkey(dt1, urn) # this is after I had used setkey(dt1, urn, date) to order dt1 properly
setkey(dt2, urn)

dt2[dt1[,list(lastchangedate=max(date[which(diff(amount)!=0)+1])), 
    by=urn],lastchangedate:=i.lastchangedate]

1 个答案:

答案 0 :(得分:1)

这应该这样做:

setkey(dt1, urn, date)   ## sort table
dt2 <- dt1[, list(lastamount=amount[.N],
                  lastchangedate=max(date[which(diff(amount)!=0)+1])), 
           by=urn]

最后一行查看diff(amount) != 0的位置并将其偏移一个(发生更改的行的索引)并返回最新日期(max)。