'Subset continuous data in one dataframe around field calibration in a second dataframe

I'm trying to subset continuous data to find the last real record before field calibration and the first real record after relaunch so that I can compare the data before and after calibration. In order to attempt to find the real records I'm trying to subset an hour on either side of the field calibration. I have two dataframes, one with the continuous data, and one with the field calibration data.

Continuous dataframe looks like this (pals.control.sub in code):

DateTime2 DO TEMP FLAG
2021-06-30 10:30:00 7.07 25.1 NO FLAG
2021-06-30 11:00:00 6.94 25.1 NO FLAG

Field Calibration dataframe looks like this (pals.control.fc in code):

Date2 YSI.DO YSI.TEMP DO.calc DO.conc New Gain
2021-06-09 11:58:00 8.05 24.6 1.029
2021-06-30 13:38:00 7.10 27.7 5.99 6.14 1.055

Here's what I've tried:

pals.control.fc$fc.high<-pals.control.fc$DATE2+hours(2)
pals.control.fc$fc.low<-pals.control.fc$DATE2-hours(2)
pals.control.nn.fc<-subset(pals.control.sub,(DateTime2 >= pals.control.fc$fc.high &
DateTime2<=pals.control.fc$fc.low)) 

But that wasn't giving me the full range of values between the "low" and "high" times, it was giving me records that were close to the "low" and "high" times but nothing in between.

I got this code to work, but it's clunky and I need it to be less manual because I have a lot of these dataframes to check.

pals.control.nn.fc<-subset(pals.control.sub,c(DateTime2 >= '2021-06-09 10:58:00' & 
                                                DateTime2<='2021-06-09 12:58:00')|
                                             (DateTime2 >= '2021-06-30 12:38:00' & 
                                                DateTime2<='2021-06-30 14:38:00')|
                            (DateTime2 >= '2021-07-19 11:28:00' & 
                                                DateTime2<='2021-07-19 13:28:00')|
                             (DateTime2 >= '2021-08-11 12:40:00' & 
                                                DateTime2<='2021-08-11 14:40:00')|
                             (DateTime2 >= '2021-08-26 14:12:00' & 
                                                DateTime2<='2021-08-26 16:12:00')|
                             (DateTime2 >= '2021-10-06 14:38:00' & 
                                                DateTime2<='2021-10-06 16:38:00')|
                             (DateTime2>= '2021-10-29 12:20:00'&DateTime2<='2021-10-29 14:20:00'))

Happy to answer questions.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source