如何在R中根据时间和缓冲区间合并不等长的数据帧?

我试图合并两个数据帧,其中共同点是时间.但是,两者之间的时间记录可能不同.我希望按时间合并这两个,但缓冲间隔为30分钟.

数据框在概念上设置如下:

Data_cam <- data.frame(Start_haul=c(("31-10-2015  07:13:00"),("31-10-2015  22:40:00"),("01-11-2015  06:48:00"),("01-11-2015  16:13:00")), 
              VesselID=c('XBBX','XBBX','XAAX','XAAX'),
              Species=("TOR"), Discard=c(0.28,0.96,2.92,0)) 

Data_sif <- data.frame(Start_haul=c(("31-10-2015  07:05:00"),("31-10-2015  07:05:00"),("31-10-2015  07:05:00"),("31-10-2015  23:05:00"),("31-10-2015  23:05:00"),("01-11-2015  06:28:00"),("01-11-2015  06:28:00"),("01-11-2015  06:28:00"),("01-11-2015  16:11:00")),             VesselID=c('XBBX','XBBX','XBBX','XBBX','XBBX','XAAX','XAAX','XAAX','XAAX'),Species=("TOR"), Size_class=c("1","2","3","4","5","1","2","4","5"),  Landing_kg=c(10.5,20.5,5.6,400,2,120,250,10.3,2.1))

这意味着Data_sif中的三个第一行与Data_cam中的第一行匹配,我想将Data_cam中第一行的“Discard” – 值列添加到Data_sif中的第三行.
同样,Data_sif中的第4行和第5行与Data_cam中的第二行匹配,我想在此处添加“Discard”,依此类推所有行.
“Discard”列中的值应重复显示在“Size_class”列中显示的公共时间戳的每个值.

所需的输出看起来像这样

Data_combined <- data.frame(Start_haul=c(("31-10-2015  07:05:00"),("31-10-2015  07:05:00"),("31-10-2015  07:05:00"),("31-10-2015  23:05:00"),("31-10-2015  23:05:00"),("01-11-2015  06:28:00"),("01-11-2015  06:28:00"),("01-11-2015  06:28:00"),("01-11-2015  16:11:00")),             VesselID=c('XBBX','XBBX','XBBX','XBBX','XBBX','XAAX','XAAX','XAAX','XAAX'),Species=("TOR"), Size_class=c("1","2","3","4","5","1","2","4","5"),  Landing_kg=c(10.5,20.5,5.6,400,2,120,250,10.3,2.1), 
Discard=c(0.28,0.28,0.28,0.96,0.96,2.92,2.92,2.92,0))

我想在最终实现中添加更多列,包括位置数据,但为了简单起见,我想从合并Discard-column开始.

我已经尝试过旧帖子但是没能为我的数据实现它.

最佳答案
使用sqldf可以实现一个解决方案.

library(sqldf)

# First convert Start_haul  to Date/time
Data_cam$Start_haul <- as.POSIXct(Data_cam$Start_haul, 
          format = "%d-%m-%Y %H:%M:%S")

Data_sif$Start_haul <- as.POSIXct(Data_sif$Start_haul, 
      format = "%d-%m-%Y %H:%M:%S")

# The absolute difference between Start_haul is considered as less than
#  30*60 (1800 seconds) for joining.     

sqldf("SELECT Data_sif.Start_haul, Data_sif.VesselID, Data_sif.Species,
       Data_sif.Size_class, Data_sif.Landing_kg, Data_cam.Discard
       FROM Data_sif, Data_cam
       WHERE Data_sif.VesselID = Data_cam.VesselID AND
       Data_sif.Species = Data_cam.Species AND
       abs(Data_sif.Start_haul - Data_cam.Start_haul) <= 30*60
      ")

# Result 
#           Start_haul VesselID Species Size_class Landing_kg Discard
#1 31-10-2015  07:05:00     XBBX     TOR          1       10.5    0.28
#2 31-10-2015  07:05:00     XBBX     TOR          2       20.5    0.28
#3 31-10-2015  07:05:00     XBBX     TOR          3        5.6    0.28
#4 31-10-2015  23:05:00     XBBX     TOR          4      400.0    0.96
#5 31-10-2015  23:05:00     XBBX     TOR          5        2.0    0.96
#6 01-11-2015  06:28:00     XAAX     TOR          1      120.0    2.92
#7 01-11-2015  06:28:00     XAAX     TOR          2      250.0    2.92
#8 01-11-2015  06:28:00     XAAX     TOR          4       10.3    2.92
#9 01-11-2015  16:11:00     XAAX     TOR          5        2.1    0.00

数据

Data_cam <- data.frame(Start_haul=c(("31-10-2015  07:13:00"),("31-10-2015  22:40:00"),("01-11-2015  06:48:00"),("01-11-2015  16:13:00")), 
                       VesselID=c('XBBX','XBBX','XAAX','XAAX'),
                       Species=("TOR"), Discard=c(0.28,0.96,2.92,0)) 

Data_sif <- data.frame(Start_haul=c(("31-10-2015  07:05:00"),("31-10-2015  07:05:00"),("31-10-2015  07:05:00"),("31-10-2015  23:05:00"),("31-10-2015  23:05:00"),("01-11-2015  06:28:00"),("01-11-2015  06:28:00"),("01-11-2015  06:28:00"),("01-11-2015  16:11:00")),             VesselID=c('XBBX','XBBX','XBBX','XBBX','XBBX','XAAX','XAAX','XAAX','XAAX'),Species=("TOR"), Size_class=c("1","2","3","4","5","1","2","4","5"),  Landing_kg=c(10.5,20.5,5.6,400,2,120,250,10.3,2.1))

转载注明原文:如何在R中根据时间和缓冲区间合并不等长的数据帧? - 代码日志