具有两个条件的Inner_join和区间条件内的间隔

时间:2018-05-07 11:25:26

标签: r time dplyr inner-join intervals

尝试根据多个条件和时间间隔条件连接2个数据帧,如下例所示:

# two sample dataframes with time intervals
df1 <- data.frame(key1 = c("a", "b", "c", "d", "e"),
                   key2 = c(1:5),
                   time1 = as.POSIXct(hms::as.hms(c("00:00:15", "00:15:15", "00:30:15", "00:40:15", "01:10:15"))),
                   time2 = as.POSIXct(hms::as.hms(c("00:05:15", "00:20:15", "00:35:15", "00:45:15", "01:15:15")))) %>% 
  mutate(t1 = interval(time1, time2)) %>%
  select(key1, key2, t1)  

df2 <- data.frame(key1 = c("b", "c", "a", "e", "d"),
                   key2 = c(2, 6, 1, 8, 5),
                   sam1 = as.POSIXct(hms::as.hms(c("00:21:15", "00:31:15", "00:03:15", "01:20:15", "00:43:15"))),
                   sam2 = as.POSIXct(hms::as.hms(c("00:23:15", "00:34:15", "00:04:15", "01:25:15", "00:44:15")))) %>% 
mutate(t2 = interval(sam1, sam2)) %>%
select(key1, key2, t2)

需要对应的第一件事是列key1key2,这可以通过以下方式完成(产生错误):

df <- inner_join(df1, df2, by = c("key1", "key2"))

但是,在加入时需要检查另外一个条件,即间隔t2是否在t1之内。我可以这样手动执行此操作:

 df$t2 %within% df$t1

我猜错误来自于使用间隔加入数据帧,这可能不是正确的方法,这就是错误的原因。

# desired dataframe
df <- data.frame(key1 = c("a", "b"), key2 = c(1,2), time_condition = c(TRUE, FALSE))

如果t1来自"00:00:15" to "00:05:15",那么"00:03:15" to "00:04:15"的对应t2将在区间t1内。如果t2在t1之内,这将导致time_condition列为TRUE,否则为FALSE。

3 个答案:

答案 0 :(得分:3)

使用data.table,您可以在加入时执行操作。这是一个例子

library(data.table)
df2[df1, # left join
    .(time_condition = sam1 > time1 & sam2 < time2), # condition while joining
    on = .(key1, key2), # keys
    by = .EACHI, # check condition per join
    nomatch = 0L] # make it an inner join

#    key1 key2 time_condition
# 1:    a    1           TRUE
# 2:    b    2          FALSE
# your data generated using data.table

df1 <- data.table(key1 = c("a", "b", "c", "d", "e"),
                  key2 = c(1:5),
                  time1 = as.ITime(c("00:00:15", "00:15:15", "00:30:15", "00:40:15", "01:10:15")),
                  time2 = as.ITime(c("00:05:15", "00:20:15", "00:35:15", "00:45:15", "01:15:15"))) 
df2 <- data.table(key1 = c("b", "c", "a", "e", "d"),
                  key2 = c(2, 6, 1, 8, 5),
                  sam1 = as.ITime(c("00:21:15", "00:31:15", "00:03:15", "01:20:15", "00:43:15")),
                  sam2 = as.ITime(c("00:23:15", "00:34:15", "00:04:15", "01:25:15", "00:44:15")))

答案 1 :(得分:1)

这个怎么样?

library(dplyr)

df1 %>%
  inner_join(df2, by = c("key1", "key2")) %>%
  filter(sam1 >= time1 & sam1 <= time2 & sam2 >= time1 & sam2 <= time2) %>%
  mutate(t1 = interval(time1, time2),
         t2 = interval(sam1, sam2)) %>%
  select(key1, key2, t1, t2)

输出为:

  key1 key2                                               t1                                               t2
1    a    1 1970-01-01 00:00:15 UTC--1970-01-01 00:05:15 UTC 1970-01-01 00:03:15 UTC--1970-01-01 00:04:15 UTC

示例数据:

df1 <- data.frame(key1 = c("a", "b", "c", "d", "e"),
                  key2 = c(1:5),
                  time1 = as.POSIXct(hms::as.hms(c("00:00:15", "00:15:15", "00:30:15", "00:40:15", "01:10:15"))),
                  time2 = as.POSIXct(hms::as.hms(c("00:05:15", "00:20:15", "00:35:15", "00:45:15", "01:15:15"))))

df2 <- data.frame(key1 = c("b", "c", "a", "e", "d"),
                  key2 = c(2, 6, 1, 8, 5),
                  sam1 = as.POSIXct(hms::as.hms(c("00:21:15", "00:31:15", "00:03:15", "01:20:15", "00:43:15"))),
                  sam2 = as.POSIXct(hms::as.hms(c("00:23:15", "00:34:15", "00:04:15", "01:25:15", "00:44:15"))))

答案 2 :(得分:1)

您可以使用内置函数merge()进行连接。

df =  merge(df1, df2, by = c("key1", "key2"))
df = data.frame(df[,c("key1", "key2")], time_condition = df$t2 %within% df$t1)
df
#  key1 key2 time_condition
#1    a    1           TRUE
#2    b    2          FALSE

谢谢