简体   繁体   中英

How to compare a DateTime in one Dataframe to a Start and End Time in Another Data frame using tidy R?

I have a dataframe of conditions that are time based. I would like to compare the time in a set of data to the start and end times in the conditions to give the correct condition. I was thinking of using purrr to map a function to do this, but I'm stuck.

The data and an example output are below:

>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

What I'm trying to do is get the following:

> 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

My brain has given up. Can someone point me in the right direction?

Thank you kindly!

Shawn Way

Using fuzzyjoin package may help -

fuzzyjoin::fuzzy_inner_join(data, conditions, 
                            by = c('DateTime' = 'Start', 'DateTime' = 'End'), 
                            match_fun = c(`>=`, `<=`))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM