【发布时间】:2021-11-02 21:01:11
【问题描述】:
我有 2 个数据框,如下所示:
Dataframe 1: traffic_df,每小时数据。
| Date_Time | Traffic |
|---|---|
| 2020-03-09 06:00:00 | 10 |
| 2020-03-09 07:00:00 | 20 |
| 2020-03-10 07:00:00 | 20 |
| 2020-03-24 08:00:00 | 15 |
数据框 2: Alert.level
| Start | End | Alert.level |
|---|---|---|
| 10/03/2020 13:30 | 23/03/2020 13:30 | 2 |
| 23/03/2020 13:30 | 25/03/2020 23:59 | 3 |
我想在 traffic_df 中添加第三列,如果 Date_Time 在 Start Alert.level df 的 和 End Date_Time,以便生成的数据框如下所示:
数据框 1: traffic_df
| Date_Time | Traffic | Alert.Level |
|---|---|---|
| 2020-03-09 06:00:00 | 10 | |
| 2020-03-09 07:00:00 | 20 | |
| 2020-03-10 07:00:00 | 20 | 2 |
| 2020-03-24 08:00:00 | 15 | 3 |
有没有办法做到这一点而不必制作一个匹配的每小时数据框然后使用连接? 我正在考虑以某种方式使用地图功能?
生成df的代码:
traffic_df <- structure(list(Date_Time = c("2020-03-09 06:00:00", "2020-03-09 07:00:00", "2020-03-10 07:00:00",
"2020-03-24 08:00:00"), Traffic = c(10L, 20L, 20L, 15L)),
row.names = c(NA, -4L), class = "data.frame")
Alert.Level = data.frame(Start = c("10/03/2020 13:30", "23/03/2020 13:30"),
End = c("23/03/2020 13:30", "25/03/2020 23:59"),
Alert.level = c(2, 3))
【问题讨论】:
标签: r dataframe conditional-statements purrr