[英]How to compare a DateTime in one Dataframe to a Start and End Time in Another Data frame using tidy R?
我有一個基於時間的條件的 dataframe。 我想將一組數據中的時間與條件中的開始和結束時間進行比較,以給出正確的條件。 我正在考慮使用 purrr 到 map 和 function 來執行此操作,但我被卡住了。
數據和示例 output 如下:
>library(tidyverse)
>library(lubridate)
>conditions <- tibble(Condition=c("Startup","Precondition","Heating Up","Exposure","Postcondition"),
Start=ymd_hms(c("2021-12-22 19:05:00","2021-12-22 19:26:00","2021-12-22 19:35:00",
"2021-12-22 19:39:30","2021-12-22 20:04:30")),
End=ymd_hms(c("2021-12-22 19:26:00","2021-12-22 19:35:00","2021-12-22 19:39:30",
"2021-12-22 20:04:30","2021-12-22 22:09:30"))
)
>data <- tibble(DateTime=ymd_hms(c("2021-12-22 19:05:00","2021-12-22 19:05:30","2021-12-22 19:06:00",
"2021-12-22 19:06:30","2021-12-22 19:07:00","2021-12-22 19:07:30")),
R57827=c(21.1,20.8,20.7,20.5,20.4,20.3))
> conditions
# A tibble: 5 x 3
Condition Start End
<chr> <dttm> <dttm>
1 Startup 2021-12-22 19:05:00 2021-12-22 19:26:00
2 Precondition 2021-12-22 19:26:00 2021-12-22 19:35:00
3 Heating Up 2021-12-22 19:35:00 2021-12-22 19:39:30
4 Exposure 2021-12-22 19:39:30 2021-12-22 20:04:30
5 Postcondition 2021-12-22 20:04:30 2021-12-22 22:09:30
> head(data)
# A tibble: 6 x 8
DateTime R57827
<dttm> <dbl>
1 2021-12-22 19:05:00 21.1
2 2021-12-22 19:05:30 20.8
3 2021-12-22 19:06:00 20.7
4 2021-12-22 19:06:30 20.5
5 2021-12-22 19:07:00 20.4
6 2021-12-22 19:07:30 20.3
我想要做的是得到以下內容:
> head(data)
# A tibble: 6 x 8
DateTime R57827 Condition
<dttm> <dbl> <chr>
1 2021-12-22 19:05:00 21.1 Startup
2 2021-12-22 19:26:30 20.8 Precondition
3 2021-12-22 19:35:00 20.7 Precondition
4 2021-12-22 19:35:30 20.5 Heating Up
5 2021-12-22 19:45:00 20.4 Exposure
6 2021-12-22 20:05:30 20.3 Postcondition
我的大腦已經放棄了。 有人可以指出我正確的方向嗎?
非常感謝你!
肖恩路
使用模糊連接fuzzyjoin
可能會有所幫助 -
fuzzyjoin::fuzzy_inner_join(data, conditions,
by = c('DateTime' = 'Start', 'DateTime' = 'End'),
match_fun = c(`>=`, `<=`))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.