How to select time range in POSIXct format when time range spans two dates?

Viewed 1045

I am trying to select a range of time that spans across 2 days (e.g. 2011-01-01 17:00:00 to 2011-01-02 05:00:00) to compute an average of some variable (e.g. wind speed) during that time interval.

Sample data:

## Create a record and some random data for every 15 min 
## over 10 days for windspeed.
dates <- seq(as.POSIXct("2011-01-01 00:00:00", tz = "GMT"),
         as.POSIXct("2011-01-10 23:59:00", tz = "GMT"),
         by = 900)
windspeed <- sample(0:20, length(dates), replace = TRUE)
Data  <- data.frame(dates = dates, windspeed = windspeed)

I was able to filter the dates I was interested in from my dataset using the dplyr package:

Data %>%
filter(dates > ymd_hms("2011-01-01 00:00:00", tz = GMT) & dates < ymd_hms("2011-01-04 00:00:00", tz = GMT) | dates > ymd_hms("2011-01-06 00:00:00", tz = GMT) & dates < ymd_hms("2011-01-010 00:00:00", tz = GMT))

Is there a wildcard that I could use in place of the DATE in the above code and only specify the time interval of interest? Or some other method of computing the average over a time interval regardless of the date?

1 Answers
Related