I have two dataframes,
I would like to obtain the average of speed variables (data1 dataframe) for the 3 hours preceding the date in my dataframe 2 (data2 dataframe)
For example i would like the mean of the speed between the date 2021-02-15 01:00:00 and the date find in my dataframe 2: 2021-02-15 04:00:00 (- 3 hours); an other example is between the 2021-02-15 07:00:00 GMT and the 2021-02-15 10:00:00...
Here is my two dataframes:
Speed <- c(7,9,9,4,2,5,2,NA,1,14,3,4,10,9,34,14,25)
Date <- c("2021-02-15 01:00:00 GMT","2021-02-15 02:00:00 GMT","2021-02-15 03:00:00 GMT","2021-02-15 04:00:00 GMT", "2021-02-15 05:00:00 GMT" ,"2021-02-15 06:00:00 GMT","2021-02-15 07:00:00 GMT","2021-02-15 08:00:00 GMT", "2021-02-15 09:00:00 GMT","2021-02-15 10:00:00 GMT","2021-02-15 11:00:00 GMT" ,"2021-02-15 12:00:00 GMT","2021-02-15 13:00:00 GMT","2021-02-15 14:00:00 GMT", "2021-02-15 15:00:00 GMT","2021-02-15 16:00:00 GMT", "2021-02-15 17:00:00 GMT")
Date <-as.POSIXlt(Date,tz="GMT",format="%Y-%m-%d %H:%M:%S")
data1 <- data.frame(Speed, Date)
search <- as.Date(c("2021-02-15 04:00:00","2021-02-15 10:00:00","2021-02-15 17:00:00"))
search <-as.POSIXlt(search ,tz="GMT",format="%Y-%m-%d %H:%M:%S")
data2 <- data.frame(search)
How can I do?
I know lubridate is a good library for working on dates and I would like to find a solution using it, but I couldn't figure out how to do.
My expected output will be:
Dates average Speed
2021-02-15 04:00:00 average speed between this date and -3hours
2021-02-15 10:00:00 average speed between this date and -3hours
2021-02-15 17:00:00 average speed between this date and -3hours
I have a big dataset