r按ID和两个日期之间的日期合并 [英] r merge by id and date between two dates

查看:74
本文介绍了r按ID和两个日期之间的日期合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 dataset1 ,其中有两列IDApplication_SubmittedDate. Application_SubmittedDate列是日期/时间列.

I have dataset1 which has two columns ID and Application_SubmittedDate. The Application_SubmittedDate column is a date/time column.

     ID         Application_SubmittedDate
     6972        2001-05-30 16:57:00
     6972        2003-03-08 12:30:00
     6972        2006-03-22 17:43:00
     6972        2003-08-07 20:20:00
     6972        2006-07-28 18:28:00
     6972        2001-05-25 17:14:00
     6972        2003-09-30 00:48:00
     6972        2002-06-04 18:11:00
     6972        2006-05-06 17:30:00
     6972        2003-02-24 16:02:00
     6972        2006-09-16 16:29:00
     6972        2003-02-12 22:47:00
     6972        2002-08-15 23:30:00
     6972        2002-08-31 22:32:00
     40841       2002-09-27 05:39:00
     40841       2002-01-08 09:05:00
     40841       2002-10-07 21:04:00
     40841       2002-08-17 18:50:00
     59547       2003-08-12 10:45:00
     59547       2001-02-20 17:02:00
     59547       2002-11-05 23:01:00
     60861       2003-10-27 14:40:00
     63457       2001-12-05 04:16:00
     65048       2002-12-16 10:18:00
     65048       2003-12-29 17:52:00
     65048       2005-02-20 16:58:00
     67037       2004-01-01 18:18:00
     67037       2006-06-22 01:04:00
     67037       2004-07-31 18:30:00
     67037       2004-08-04 14:09:00
     67037       2005-04-20 18:06:00
     67037       2006-06-15 16:55:00

df1 <- structure(list(ID = c(6972L, 6972L, 6972L, 6972L, 6972L, 6972L, 
6972L, 6972L, 6972L, 6972L, 6972L, 6972L, 6972L, 6972L, 40841L, 
40841L, 40841L, 40841L, 59547L, 59547L, 59547L, 60861L, 63457L, 
65048L, 65048L, 65048L, 67037L, 67037L, 67037L, 67037L, 67037L, 
67037L), Application_SubmittedDate = structure(c(991241820, 1047126600, 
1143049380, 1060287600, 1154111280, 990810840, 1064882880, 1023214260, 
1146936600, 1046102520, 1158424140, 1045090020, 1029454200, 1030833120, 
1033105140, 1010480700, 1034024640, 1029610200, 1060685100, 982688520, 
1036537260, 1067265600, 1007525760, 1040033880, 1072720320, 1108918680, 
1072981080, 1150938240, 1091298600, 1091628540, 1114020360, 1150390500
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("ID", 
"Application_SubmittedDate"), class = "data.frame", row.names = c(1L, 
18L, 35L, 52L, 69L, 86L, 103L, 137L, 154L, 188L, 205L, 239L, 
256L, 273L, 290L, 300L, 305L, 310L, 315L, 327L, 339L, 351L, 352L, 
353L, 359L, 371L, 389L, 400L, 411L, 422L, 466L, 477L))

第二个数据集具有三列IDApplication_ProcessStartDateApplication_ProcessEndDate.这两个Applicateion ProcessStarDate和EndDate列是日期/时间列.

The second dataset is has three columns ID , Application_ProcessStartDate, Application_ProcessEndDate. These two Applicateion ProcessStarDate and EndDate columns are date/time columns.

    ID     Application_ProcessStartDate Application_ProcessEndDate
    65048  2005-02-20 12:44:22          2005-02-23 06:07:45       
    65048  2006-06-21 17:31:45          2006-06-24 01:42:41       
    111993 2006-06-21 17:31:45          2006-06-24 01:42:41      




    df2 <- structure(list(ID = c(65048L, 65048L, 111993L), Application_ProcessStartDate = structure(c(1108903462, 
1150911105, 1150911105), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Application_ProcessEndDate = structure(c(1109138865, 1151113361, 
    1151113361), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("ID", 
"Application_ProcessStartDate", "Application_ProcessEndDate"), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

我的目标是首先合并ID)2)在这些ID中合并来自df1的ID,其中Application_SubmittedDate值在Application_ProcessStartDateApplication_ProcessEndDate值之间.

What I am aiming for is to merge First) By ID 2) Within those ID merge those from df1 where Application_SubmittedDate value is between Application_ProcessStartDate, Application_ProcessEndDate values.

最终结果看起来像这样

         ID         Application_SubmittedDate   Application_ProcessStartDate    Application_ProcessEndDate
         6972        2001-05-30 16:57:00
         6972        2003-03-08 12:30:00
         6972        2006-03-22 17:43:00
         6972        2003-08-07 20:20:00
         6972        2006-07-28 18:28:00
         6972        2001-05-25 17:14:00
         6972        2003-09-30 00:48:00
         6972        2002-06-04 18:11:00
         6972        2006-05-06 17:30:00
         6972        2003-02-24 16:02:00
         6972        2006-09-16 16:29:00
         6972        2003-02-12 22:47:00
         6972        2002-08-15 23:30:00
         6972        2002-08-31 22:32:00
         40841       2002-09-27 05:39:00
         40841       2002-01-08 09:05:00
         40841       2002-10-07 21:04:00
         40841       2002-08-17 18:50:00
         59547       2003-08-12 10:45:00
         59547       2001-02-20 17:02:00
         59547       2002-11-05 23:01:00
         60861       2003-10-27 14:40:00
         63457       2001-12-05 04:16:00
         65048       2002-12-16 10:18:00
         65048       2003-12-29 17:52:00         
         65048       2005-02-20 16:58:00         2005-02-20 12:44:22          2005-02-23 06:07:45  
         65048            NA                     2006-06-21 17:31:45          2006-06-24 01:42:41 
         67037       2004-01-01 18:18:00
         67037       2006-06-22 01:04:00
         67037       2004-07-31 18:30:00
         67037       2004-08-04 14:09:00
         67037       2005-04-20 18:06:00
         67037       2006-06-15 16:55:00
         111993        NA                        2006-06-21 17:31:45          2006-06-24 01:42:41

我尝试过foverlaps,它不处理日期/时间值,仅处理日期值,因此被排除在外.我还尝试了sqldf库中的JOIN,但这仅适用于INNER JOINS,而不适用于OUTER JOINS,因此也被排除在外.不确定如何完成此操作.任何帮助或建议,我们将不胜感激.

I have tried foverlaps this does not handle date/time values, only date values so this is ruled out. I also tried JOIN from sqldf library but this only does INNER JOINS , not OUTER JOINS so this is also ruled out. Not sure how to accomplish this. Any help or advise is much appreciated.

推荐答案

使用data.table可以实现另一种解决方案.该方法将对right_joinleft_join都加入df1df2,然后将两者合并.

Another solution can be achieved using data.table. The approach will be join df1 and df2 for both right_join and left_join and then merge the both.

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


rhs_join <- df1[df2, 
                .(i.ID, x.Application_SubmittedDate, i.Application_ProcessStartDate, 
                  i.Application_ProcessEndDate),
                on = .(ID = ID, Application_SubmittedDate >= Application_ProcessStartDate,
                       Application_SubmittedDate <= Application_ProcessEndDate)][,.(ID = i.ID, Application_SubmittedDate = x.Application_SubmittedDate,
    Application_ProcessStartDate = i.Application_ProcessStartDate,
    Application_ProcessEndDate = i.Application_ProcessEndDate)]

lhs_join <- df2[df1, 
                .(i.ID, Application_SubmittedDate, x.Application_ProcessStartDate,
                  x.Application_ProcessEndDate),
                on = .(ID = ID, Application_ProcessStartDate <= Application_SubmittedDate,
                       Application_ProcessEndDate >= Application_SubmittedDate)][,.(ID = i.ID, Application_SubmittedDate = Application_SubmittedDate,
    Application_ProcessStartDate = x.Application_ProcessStartDate,
    Application_ProcessEndDate = x.Application_ProcessEndDate)]


#Merge both data.frames
merge(rhs_join, lhs_join, all=TRUE)

结果

ID Application_SubmittedDate Application_ProcessStartDate Application_ProcessEndDate
 1:   6972       2001-05-25 17:14:00                         <NA>                       <NA>
 2:   6972       2001-05-30 16:57:00                         <NA>                       <NA>
 3:   6972       2002-06-04 18:11:00                         <NA>                       <NA>
.....
.....
.....
23:  63457       2001-12-05 04:16:00                         <NA>                       <NA>
24:  65048                      <NA>          2006-06-21 17:31:45        2006-06-24 01:42:41
25:  65048       2002-12-16 10:18:00                         <NA>                       <NA>
26:  65048       2003-12-29 17:52:00                         <NA>                       <NA>
27:  65048       2005-02-20 16:58:00          2005-02-20 12:44:22        2005-02-23 06:07:45
28:  67037       2004-01-01 18:18:00                         <NA>                       <NA>
29:  67037       2004-07-31 18:30:00                         <NA>                       <NA>
30:  67037       2004-08-04 14:09:00                         <NA>                       <NA>
31:  67037       2005-04-20 18:06:00                         <NA>                       <NA>
32:  67037       2006-06-15 16:55:00                         <NA>                       <NA>
33:  67037       2006-06-22 01:04:00                         <NA>                       <NA>
34: 111993                      <NA>          2006-06-21 17:31:45        2006-06-24 01:42:41

这篇关于r按ID和两个日期之间的日期合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆