按潮汐状态分类日期/时间

时间:2016-12-26 17:52:15

标签: r date datetime time

我想根据df1中给出的潮汐状态对df2日期/时间进行分类。与df2相关的是它给出了潮汐最高时的准确时间HIGHLOW

我想更改它,以便根据HIGHLOW潮汐两侧的3小时间隔对潮汐高度进行分类。

 df1 <- data.frame(date_time = as.POSIXct(c("2015-09-06 09:00:00 BST", "2015-09-06 09:48:00 BST", "2015-09-06 10:37:00 BST", "2015-09-06 12:00:00 BST", "2015-09-06 12:48:00 BST","2015-09-06 13:35:00 BST")))

> df1
            date_time
1 2015-09-06 09:00:00
2 2015-09-06 09:48:00
3 2015-09-06 10:37:00
4 2015-09-06 12:00:00
5 2015-09-06 12:48:00
6 2015-09-06 13:35:00

df2 <- data.frame(Date_time = as.POSIXct(c("2015-09-06 07:09:00 GMT", "2015-09-06 13:18:00 GMT", "2015-09-06 19:52:00 GMT", "2015-09-07 02:01:00 GMT", "2015-09-07 08:28:00 GMT", "2015-09-07 14:43:00 GMT")), Tide_state = c("LOW",  "HIGH", "LOW",  "HIGH", "LOW",  "HIGH"))

df2
            Date_time Tide_state
1 2015-09-06 07:09:00        LOW
2 2015-09-06 13:18:00       HIGH
3 2015-09-06 19:52:00        LOW
4 2015-09-07 02:01:00       HIGH
5 2015-09-07 08:28:00        LOW
6 2015-09-07 14:43:00       HIGH

我尝试过使用findInterval()

df1$tides <- df2$Tide_state[findInterval(x = df1$date_time, vec = df2$Date_time)]

结果:

df1
          date_time tides
1 2015-09-06 09:00:00   LOW
2 2015-09-06 09:48:00   LOW
3 2015-09-06 10:37:00   LOW
4 2015-09-06 12:00:00   LOW
5 2015-09-06 12:48:00   LOW
6 2015-09-06 13:35:00  HIGH

期望的结果:(基于潮汐高低各3小时。

df1
 date_time tides
    1 2015-09-06 09:00:00   LOW
    2 2015-09-06 09:48:00   LOW
    3 2015-09-06 10:37:00   HIGH
    4 2015-09-06 12:00:00   HIGH
    5 2015-09-06 12:48:00   HIGH
    6 2015-09-06 13:35:00   HIGH

非常感谢任何帮助

0 个答案:

没有答案