The Data:
I have a data set (df1) with a start date and end date for each observation (the actual data has ~50,000 observations).
dateStart <- c("2018-01-23","2017-11-01","2017-11-29")
dateEnd <- c("2018-01-25","2017-11-02","2017-11-30")
obs <- c(1,2,3)
dateStart <- as.Date(as.character(dateStart), format = "%Y-%m-%d")
dateEnd <- as.Date(as.character(dateEnd), format = "%Y-%m-%d")
df1 <- data.frame(obs,dateStart,dateEnd)
df1
obs dateStart dateEnd
1 2018-01-23 2018-01-25
2 2017-11-01 2017-11-02
3 2017-11-29 2017-11-30
The other data set (df2) has recorded values (here the data is >150,000 rows of data):
datetime <- c("2018-01-23 14:30:00", "2018-01-23 15:30:00","2017-11-01 12:10:00","2017-11-01 22:59:00","2017-11-29 00:40:00", "2017-11-29 16:50:00")
value <- c(1.1,1.2,2.1,2.2,3.1,3.2)
date <- as.POSIXct(as.character(datetime), format = "%Y-%m-%d %H:%M:%S")
df2 <- data.frame(datetime,value)
df2
datetime value
2018-01-23 14:30:00 1.1
2018-01-23 15:30:00 1.2
2017-11-01 12:10:00 2.1
2017-11-01 22:59:00 2.2
2017-11-29 00:40:00 3.1
2017-11-29 16:50:00 3.2
The Question:
How do I insert the peak value from df2 which occurs between the start and end dates in df1? It should look like:
obs dateStart dateEnd value
1 2018-01-23 2018-01-25 1.2
2 2017-11-01 2017-11-02 2.2
3 2017-11-29 2017-11-30 3.2
I have used subsetting in a single data frame, but have no idea how to do it between ranges over multiple rows across two data frames.
Any help much appreciated.