One option is to create sequence 2
hours basis and then filter for time-range as:
library(lubridate)
v <- seq(from = as.POSIXct("2017-01-01 11:00"),
to = as.POSIXct("2017-01-05 21:00"), by = "2 hour")
v[hour(v)>=9 & hour(v)<=21]
# [1] "2017-01-01 11:00:00 GMT" "2017-01-01 13:00:00 GMT" "2017-01-01 15:00:00 GMT"
# [4] "2017-01-01 17:00:00 GMT" "2017-01-01 19:00:00 GMT" "2017-01-01 21:00:00 GMT"
# [7] "2017-01-02 09:00:00 GMT" "2017-01-02 11:00:00 GMT" "2017-01-02 13:00:00 GMT"
# [10] "2017-01-02 15:00:00 GMT" "2017-01-02 17:00:00 GMT" "2017-01-02 19:00:00 GMT"
# [13] "2017-01-02 21:00:00 GMT" "2017-01-03 09:00:00 GMT" "2017-01-03 11:00:00 GMT"
# [16] "2017-01-03 13:00:00 GMT" "2017-01-03 15:00:00 GMT" "2017-01-03 17:00:00 GMT"
# so on few more rows.
The above seq
generates time-series from 1st January
till 5th January
at 2 hours interval. A filter condition is applied once series has been generated. The filter condition considers only time-date for which hour >= 9
and hour <= 21
. This will provide desired time-series.