【问题标题】:Left join and select the next observation in time in R左连接并在R中及时选择下一个观察
【发布时间】:2021-09-24 18:40:09
【问题描述】:

假设我有两个数据框

df <- data.frame(ID=c("Ana", "Lola", "Ana"),
             Date=c("2020-06-06", "2020-06- 06", "2020-06- 07"),
             meat=c("fish", "poultry", "poultry"),
             time_ordered=c("2020-06-06 12:24:39", "2020-06-06 12:34:36", "2020-06-07 12:24:39"))

df2 <- data.frame(ID=c("Ana","Ana",  "Lola", "Ana"),
             Date=c("2020-06-06", "2020-06-06",  "2020-06- 06", "2020-06- 07"),
             meat=c("fish", "fish", "poultry", "poultry"),
             time_received=c("2020-06-06 12:24:40", "2020-06-06 12:26:49",  "2020-06-07 12:36:39", "2020-06-07 13:04:39"))

假设我想在 ID 和肉上加入这两个数据框。 然后,对于给定的观察,我想将 time_ordered 与它之后的第一个 time_received 匹配。 例如,我应该有一行“ID = Ana, Data= 2020-06-06, Meat = fish, time_ordered = 2020-06-06 12:24:39, time received = 2020-06-06 12:24: 40 英寸。

所以我不会将 time_received "2020-06-06 12:26:49" 与任何内容匹配。 事实上,对于每个(ID,Meat,time_observed),我想唯一匹配到(ID,Meat,min(time_received)> time_observed)

非常感谢您!

【问题讨论】:

    标签: r time tidyverse tidyr xts


    【解决方案1】:

    加入dfdf2IDmeatDate,仅保留time_received &gt; time_orderedtime_received排列数据的行,并仅保留唯一行。

    library(dplyr)
    library(lubridate)
    
    df %>%
      left_join(df2, by = c('ID', 'meat', 'Date')) %>%
      mutate(Date = ymd(Date), 
             across(c(time_ordered, time_received), ymd_hms)) %>%
      filter(time_received >  time_ordered) %>%
      arrange(ID, Date, meat, time_received) %>%
      distinct(ID, Date, meat, .keep_all = TRUE)
    
    #    ID       Date    meat        time_ordered       time_received
    #1  Ana 2020-06-06    fish 2020-06-06 12:24:39 2020-06-06 12:24:40
    #2  Ana 2020-06-07 poultry 2020-06-07 12:24:39 2020-06-07 13:04:39
    #3 Lola 2020-06-06 poultry 2020-06-06 12:34:36 2020-06-07 12:36:39
    

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 2017-01-21
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多