连接两个具有最接近时间戳记间隔的数据集

时间:2019-03-21 19:17:34

标签: r datatable dplyr timestamp

我编辑了问题-请阅读第二部分编辑2

我需要使用壁橱时间戳将两个数据集连接起来。第一个数据集是来自移动应用程序的日记数据集:

    df1 <- data.frame(stringsAsFactors=FALSE,
        datetime = c("2019-03-19T13:26:52Z", "2019-03-19T13:26:19Z",
                     "2019-03-19T13:23:46Z", "2019-03-19T13:22:20Z",
                     "2019-03-19T13:09:56Z", "2019-03-19T13:06:04Z", "2019-03-19T13:05:21Z",
                     "2019-03-19T13:04:37Z", "2019-03-19T12:47:28Z",
                     "2019-03-19T12:46:42Z"),
       transport = c("Trainride", "Trainride", "Trainride", "Trainride",
                     "Trainride", "Trainride", "Trainride", "Trainride",
                     "Trainride", "Trainride"),
              id = c("5-3", "5-3", "5-3", "5-3", "5-3", "5-3", "5-3", "5-3", "5-3",
                     "5-3"),
            disc = c("start", "stop", "start", "stop", "start", "stop", "start",
                     "stop", "start", "stop")
    )
 # datetime dttr object   
    df1 <- df1 %>% 
      mutate(datetime = lubridate::as_datetime(datetime))

此处:

              datetime transport  id  disc
1  2019-03-19 13:26:52 Trainride 5-3 start
2  2019-03-19 13:26:19 Trainride 5-3  stop
3  2019-03-19 13:23:46 Trainride 5-3 start
4  2019-03-19 13:22:20 Trainride 5-3  stop
5  2019-03-19 13:09:56 Trainride 5-3 start
6  2019-03-19 13:06:04 Trainride 5-3  stop
7  2019-03-19 13:05:21 Trainride 5-3 start
8  2019-03-19 13:04:37 Trainride 5-3  stop
9  2019-03-19 12:47:28 Trainride 5-3 start
10 2019-03-19 12:46:42 Trainride 5-3  stop

,第二个数据集是来自加速度计日志的数据集,显示了运动(= INVH)或空闲(= NIVH):

df2 <- data.frame(stringsAsFactors=FALSE,
           datetime = c("2019-03-19T23:20:00Z", "2019-03-19T23:17:30Z",
                        "2019-03-19T13:08:00Z", "2019-03-19T13:07:00Z",
                        "2019-03-19T12:38:45Z", "2019-03-19T12:32:45Z",
                        "2019-03-19T11:13:15Z", "2019-03-19T11:11:45Z", "2019-03-19T10:17:45Z",
                        "2019-03-19T10:16:45Z"),
                 id = c("5-3", "5-3", "5-3", "5-3", "5-3", "5-3", "5-3", "5-3", "5-3",
                        "5-3"),
               code = c("NIVH", "INVH", "NIVH", "INVH", "NIVH", "INVH", "NIVH",
                        "INVH", "NIVH", "INVH")
       )
 # datetime dttr object 
df2 <- df2 %>% 
  mutate(datetime = lubridate::as_datetime(datetime))

此处:

              datetime  id code
1  2019-03-19 23:20:00 5-3 NIVH
2  2019-03-19 23:17:30 5-3 INVH
3  2019-03-19 13:08:00 5-3 NIVH
4  2019-03-19 13:07:00 5-3 INVH
5  2019-03-19 12:38:45 5-3 NIVH
6  2019-03-19 12:32:45 5-3 INVH
7  2019-03-19 11:13:15 5-3 NIVH
8  2019-03-19 11:11:45 5-3 INVH
9  2019-03-19 10:17:45 5-3 NIVH
10 2019-03-19 10:16:45 5-3 INVH

我需要根据时间戳字段之间的时间差来连接两个数据帧。例如,在df1上左键连接,以查看应用程序日记数据与真实加速度计日志的协议。在大多数情况下,存在滞后时间,因此简单的左联接在这里不起作用。所以我的问题是我如何才能加入这两个数据集 根据差异为最小绝对差异的记录。

编辑2 --- @soren建议的解决方案很有帮助,但是,如果我将开始日期时间和结束时间设为间隔,将无法工作。有什么想法吗?

df1 <- df1 %>% 
  mutate(datetime = lubridate::as_datetime(datetime)) %>% 
  arrange(datetime) %>% 
  mutate(datetime_end = lead(datetime), 
         # Create an interval object.
         Travel_Interval = lubridate::interval(start = datetime, end = datetime_end))

1 个答案:

答案 0 :(得分:9)

滚动联接将完成此任务,并在最接近的日期时间联接数据框。以下使用data.table的解决方案

library(data.table)
dt1 <- as.data.table(df1)
setkeyv(dt1,"datetime")

dt2 <- as.data.table(df2)
setkeyv(dt2,"datetime")
dt2[,nearest_date:=datetime]

dt2[dt1,roll="nearest"]

> dt2[dt1,roll="nearest"]
               datetime  id code        nearest_date transport i.id  disc
 1: 2019-03-19 12:46:42 5-3 NIVH 2019-03-19 12:38:45 Trainride  5-3  stop
 2: 2019-03-19 12:47:28 5-3 NIVH 2019-03-19 12:38:45 Trainride  5-3 start
 3: 2019-03-19 13:04:37 5-3 INVH 2019-03-19 13:07:00 Trainride  5-3  stop
 4: 2019-03-19 13:05:21 5-3 INVH 2019-03-19 13:07:00 Trainride  5-3 start
 5: 2019-03-19 13:06:04 5-3 INVH 2019-03-19 13:07:00 Trainride  5-3  stop
 6: 2019-03-19 13:09:56 5-3 NIVH 2019-03-19 13:08:00 Trainride  5-3 start
 7: 2019-03-19 13:22:20 5-3 NIVH 2019-03-19 13:08:00 Trainride  5-3  stop
 8: 2019-03-19 13:23:46 5-3 NIVH 2019-03-19 13:08:00 Trainride  5-3 start
 9: 2019-03-19 13:26:19 5-3 NIVH 2019-03-19 13:08:00 Trainride  5-3  stop
10: 2019-03-19 13:26:52 5-3 NIVH 2019-03-19 13:08:00 Trainride  5-3 start

请注意,dt2[,nearest_date:=datetime]添加了附加列“ nearest_date”,因此当dt2和dt1连接在一起时,datetime的值将保留(或可用于差值计算)。而默认情况下,合并/合并数据集仅返回主键列。通常,这是理想的,因为连接的列通常是相同的。在这里,通过滚动连接就不是了,保留列或手动创建冗余列很有用,如此处所示。

还要注意,最接近的id值和datetime也可能与应用程序相关(OP中仅给出一个id值)。这是通过设置键(在连接语法中使用)来完成的:

setkeyv(dt2,c("id","datetime"))
setkeyv(dt1,c("id","datetime"))