R 从另一个数据帧按时间间隔聚合一个数据帧

R 从另一个数据帧按时间间隔聚合一个数据帧,r,data.table,aggregate,plyr,data-manipulation,R,Data.table,Aggregate,Plyr,Data Manipulation,我正在尝试聚合两个数据帧(df1和df2) 第一个包含3个变量:ID、Date1和Date2 df1 第二个变量还包含3个变量:ID、Date3和Value df2 这样做的目的是,对于每个df1行,获取具有相同ID且Date3介于Date1和Date2之间的df2$Value的总和: ID Date1 Date2 SumValue 1 2016-03-01 2016-04-01 5 1 2016-04-01 2016-05-01 14 2 2

我正在尝试聚合两个数据帧(
df1
df2

第一个包含3个变量:
ID
Date1
Date2

df1

第二个变量还包含3个变量:
ID
Date3
Value

df2

这样做的目的是,对于每个
df1
行,获取具有相同
ID
Date3
介于
Date1
Date2
之间的
df2$Value
的总和:

ID      Date1      Date2 SumValue
 1 2016-03-01 2016-04-01        5
 1 2016-04-01 2016-05-01       14
 2 2016-03-14 2016-04-15       17
 2 2016-04-15 2016-05-17        2
 3 2016-05-01 2016-06-10       14
 3 2016-06-10 2016-07-15        3

我知道如何在上面循环,但是数据帧太大了!有人有有效的解决方案吗?正在探索
数据。表
plyr
dplyr
,但找不到解决方案。

以下是使用
sapply()的基本R解决方案:


两个
data.table
解决方案应具有良好的可扩展性(以及在实现非等联接之前的良好权宜之计):

使用
by=EACHI
在J中进行比较

library(data.table)
setDT(df1)
setDT(df2)

df1[, `:=`(Date1 = as.Date(Date1), Date2 = as.Date(Date2))]
df2[, Date3 := as.Date(Date3)]

df1[  df2,
      {
        idx = Date1 <= i.Date3 & i.Date3 <= Date2
        .(Date1 = Date1[idx],
          Date2 = Date2[idx],
          Date3 = i.Date3,
          Value = i.Value)
      }, 
      on=c("ID"),
      by=.EACHI][, .(sumValue = sum(Value)), by=.(ID, Date1, Date2)]

#   ID      Date1      Date2 sumValue
# 1:  1 2016-03-01 2016-04-01        5
# 2:  1 2016-04-01 2016-05-01       14
# 3:  2 2016-03-14 2016-04-15       17
# 4:  2 2016-04-15 2016-05-17        2
# 5:  3 2016-05-01 2016-06-10       14
# 6:  3 2016-06-10 2016-07-15        3
进一步阅读


通过最近在of
data.table(v1.9.7
)中实施的
非相等连接功能,可以按如下方式完成:

dt2[dt1, .(sum = sum(Value)), on=.(ID, Date3>=Date1, Date3<=Date2), by=.EACHI]
#    ID      Date3      Date3 sum
# 1:  1 2016-03-01 2016-04-01   5
# 2:  1 2016-04-01 2016-05-01  14
# 3:  2 2016-03-14 2016-04-15  17
# 4:  2 2016-04-15 2016-05-17   2
# 5:  3 2016-05-01 2016-06-10  14
# 6:  3 2016-06-10 2016-07-15   3

dt2[dt1,(sum=sum(Value)),on=。(ID,Date3>=Date1,Date3您可以使用
数据中的
foverlaps
。表
非常感谢!两个解决方案比我预期的要多。它们对我创建的测试数据集有效!我在运行第二个解决方案时内存有问题,我正在等待第一个解决方案完成运行…超过1小时!数据太多了!你有一些关于ri和函数(ri)使用的文档吗?太好了!!!!非常感谢SymbolX!!!这正是我所需要的!!!虽然之前提出的方法花费了数小时,但您的解决方案只花费了28秒!我简直不敢相信!@user3692906-不客气。这些加入对我来说也是无价的!@user3692906-而且,对答案的“赞成票”总是很感激的;-)我很想这样做,但这是我的第一个问题,我仍然无法投票。我的声望是13岁……但我会在15岁时就这么做
df1 <- data.frame(ID=c(1L,1L,2L,2L,3L,3L),Date1=as.Date(c('2016-03-01','2016-04-01','2016-03-14','2016-04-15','2016-05-01','2016-06-01')),Date2=as.Date(c('2016-04-01','2016-05-01','2016-04-15','2016-05-17','2016-06-15','2016-07-15')));
df2 <- data.frame(ID=c(1L,1L,1L,2L,2L,2L,2L,3L,3L,3L),Date3=as.Date(c('2016-03-15','2016-04-04','2016-04-28','2016-03-18','2016-03-27','2016-04-08','2016-04-20','2016-05-05','2016-05-25','2016-06-13')),Value=c(5L,7L,7L,3L,5L,9L,2L,6L,8L,3L));
cbind(df1,SumValue=sapply(seq_len(nrow(df1)),function(ri) sum(df2$Value[df1$ID[ri]==df2$ID & df1$Date1[ri]<=df2$Date3 & df1$Date2[ri]>df2$Date3])));
##   ID      Date1      Date2 SumValue
## 1  1 2016-03-01 2016-04-01        5
## 2  1 2016-04-01 2016-05-01       14
## 3  2 2016-03-14 2016-04-15       17
## 4  2 2016-04-15 2016-05-17        2
## 5  3 2016-05-01 2016-06-15       17
## 6  3 2016-06-01 2016-07-15        3
cbind(df1,SumValue=with(expand.grid(i1=seq_len(nrow(df1)),i2=seq_len(nrow(df2))),{
    x <- df1$ID[i1]==df2$ID[i2] & df1$Date1[i1]<=df2$Date3[i2] & df1$Date2[i1]>df2$Date3[i2];
    tapply(df2$Value[i2[x]],i1[x],sum);
}));
##   ID      Date1      Date2 SumValue
## 1  1 2016-03-01 2016-04-01        5
## 2  1 2016-04-01 2016-05-01       14
## 3  2 2016-03-14 2016-04-15       17
## 4  2 2016-04-15 2016-05-17        2
## 5  3 2016-05-01 2016-06-15       17
## 6  3 2016-06-01 2016-07-15        3
library(data.table)
setDT(df1)
setDT(df2)

df1[, `:=`(Date1 = as.Date(Date1), Date2 = as.Date(Date2))]
df2[, Date3 := as.Date(Date3)]

df1[  df2,
      {
        idx = Date1 <= i.Date3 & i.Date3 <= Date2
        .(Date1 = Date1[idx],
          Date2 = Date2[idx],
          Date3 = i.Date3,
          Value = i.Value)
      }, 
      on=c("ID"),
      by=.EACHI][, .(sumValue = sum(Value)), by=.(ID, Date1, Date2)]

#   ID      Date1      Date2 sumValue
# 1:  1 2016-03-01 2016-04-01        5
# 2:  1 2016-04-01 2016-05-01       14
# 3:  2 2016-03-14 2016-04-15       17
# 4:  2 2016-04-15 2016-05-17        2
# 5:  3 2016-05-01 2016-06-10       14
# 6:  3 2016-06-10 2016-07-15        3
library(data.table)
setDT(df1)
setDT(df2)

df1[, `:=`(Date1 = as.Date(Date1), Date2 = as.Date(Date2))]
df2[, Date3 := as.Date(Date3)]

df2[, Date4 := Date3]


setkey(df1, ID, Date1, Date2)


foverlaps(df2,
          df1,
          by.x=c("ID", "Date3", "Date4"),
          type="within")[, .(sumValue = sum(Value)), by=.(ID, Date1, Date2)]

#     ID      Date1      Date2 sumValue
# 1:  1 2016-03-01 2016-04-01        5
# 2:  1 2016-04-01 2016-05-01       14
# 3:  2 2016-03-14 2016-04-15       17
# 4:  2 2016-04-15 2016-05-17        2
# 5:  3 2016-05-01 2016-06-10       14
# 6:  3 2016-06-10 2016-07-15        3
dt2[dt1, .(sum = sum(Value)), on=.(ID, Date3>=Date1, Date3<=Date2), by=.EACHI]
#    ID      Date3      Date3 sum
# 1:  1 2016-03-01 2016-04-01   5
# 2:  1 2016-04-01 2016-05-01  14
# 3:  2 2016-03-14 2016-04-15  17
# 4:  2 2016-04-15 2016-05-17   2
# 5:  3 2016-05-01 2016-06-10  14
# 6:  3 2016-06-10 2016-07-15   3