如何使用 tidy R 将一个数据框中的日期时间与另一个数据框中的开始和结束时间进行比较?
How to compare a DateTime in one Dataframe to a Start and End Time in Another Data frame using tidy R?
我有一个基于时间的条件数据框。我想将一组数据中的时间与条件中的开始和结束时间进行比较,以给出正确的条件。我正在考虑使用 purrr 映射一个函数来执行此操作,但我被卡住了。
数据和示例输出如下:
>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(`>=`, `<=`))
我有一个基于时间的条件数据框。我想将一组数据中的时间与条件中的开始和结束时间进行比较,以给出正确的条件。我正在考虑使用 purrr 映射一个函数来执行此操作,但我被卡住了。
数据和示例输出如下:
>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(`>=`, `<=`))