匹配两个数据集之间的多个变化时间段

Match multiple changing time periods between two datasets

我有两个数据帧,第一个跨越 3 个月,每 2.5 分钟记录一次深度。

shark depth temperature   datetime    date      location
A     49.5  26.2   20/03/2018 08:00 20/03/2018    SS04
A     49.5  25.3   20/03/2018 08:02 20/03/2018    SS04
A     53.0  24.2   20/03/2018 08:04 20/03/2018    SS04
A     39.5  26.5   20/03/2018 08:32 20/03/2018    Absent
A     43.0  26.2   21/03/2018 09:10 21/03/2018    Absent
A     44.5  26.5   21/03/2018 10:18 21/03/2018    SS04

我有第二个数据框,其中列出了这三个月的潮汐状态。

   date    time  depth  tide_state   datetime
18/03/2018 02:33  2.09  High    20/03/2018 02:33
18/03/2018 08:39  0.45   Low    20/03/2018 08:39
18/03/2018 14:47  2.14  High    20/03/2018 14:47
18/03/2018 20:54  0.41   Low    20/03/2018 20:54
19/03/2018 03:01  2.13  High    21/03/2019 03:01
19/03/2018 09:09  0.41   Low    21/03/2019 09:09

我想创建一个新数据集,它根据每个数据集的日期时间列为第一个数据集中的所有值插入潮汐状态。例如,如果退潮时间为 08:39,涨潮时间为 14:47,我希望 df1 中大于 08:39 但小于 14:47 的每个值都记录为 'Low',在此之后但在下一次低潮之前的值为 'High'。

由于潮汐的时间每天变化三到四次,我不太确定如何将它们合并到 R 中。我不确定是否有一种简单的方法可以使用data.table?

我将每个数据框中的两个日期时间列都设置为 POSIXct 值。

理想情况下,我想生成一个 table 像这样的数据框:

shark depth temperature   datetime    date    location tide_state
A     49.5  26.2   20/03/2018 08:00 20/03/2018  SS04     High
A     49.5  25.3   20/03/2018 08:02 20/03/2018  SS04     High
A     53.0  24.2   20/03/2018 08:04 20/03/2018  SS04     High
A     39.5  26.5   20/03/2018 08:32 20/03/2018  Absent   Low
A     43.0  26.2   20/03/2018 09:10 21/03/2018  Absent   Low  
A     44.5  26.5   20/03/2018 10:18 21/03/2018  SS04     Low

如果数据更大或连接更复杂,我建议使用 SQL 或 data.table 进行 non-equi 连接。对于这种大小的数据,您只需要 "most recent value from table2," 我们可以在 dplyr 中使用更简单的方法,我希望它会非常快。

# First some housekeeping. It will be useful to have datetimes for sorting
library(dplyr)
df1   <- df1   %>% mutate(datetime = lubridate::dmy_hm(datetime))
tides <- tides %>% mutate(datetime = lubridate::dmy_hm(datetime))

# I collate the two tables, sort by datetime, fill in the tide info, and then remove the tide rows.
df1 %>%
  bind_rows(tides %>% 
     select(datetime, tide_state, tide_depth = depth) %>%
     mutate(tide_row_to_cut = TRUE)) %>%      # EDIT
  arrange(datetime) %>%
  tidyr::fill(tide_depth, tide_state) %>%
  filter(!tide_row_to_cut) %>%                # EDIT
  select(-tide_row_to_cut)                    # EDIT

编辑:之前的版本在 Temperature 中使用 NA 删除 tide 行不适用于原始发布者,因此我在潮汐数据中添加了一个名为 [=14 的显式列=] 使修剪步骤更加稳健。

  shark depth temperature            datetime       date location tide_state tide_depth
1     A  49.5        26.2 2018-03-20 08:00:00 20/03/2018     SS04       High       2.09
2     A  49.5        25.3 2018-03-20 08:02:00 20/03/2018     SS04       High       2.09
3     A  53.0        24.2 2018-03-20 08:04:00 20/03/2018     SS04       High       2.09
4     A  39.5        26.5 2018-03-20 08:32:00 20/03/2018   Absent       High       2.09
5     A  43.0        26.2 2018-03-21 09:10:00 21/03/2018   Absent        Low       0.41
6     A  44.5        26.5 2018-03-21 10:18:00 21/03/2018     SS04        Low       0.41

我相信这符合说明,但它与请求的输出略有不同,因为低潮发生在 08:39,08:32 读数后几分钟。那时潮水会很低,但还没有达到最低点。您可能想寻找 "closest" 潮汐。实现此目的的一种方法是将潮汐的时间中途移回到先前的潮汐,或一个固定的时间(例如 2 小时?)。