rfilterdplyrtibbletime

filtering intraday data R


I'm trying to filter intraday-data to include only certain period inside the day. Is there a trick in some packages to achieve this. Here is example data:

library(tibbletime)

example <- as.tibble(data.frame(
  date = ymd_hms(seq(as.POSIXct("2017-01-01 09:00:00"), as.POSIXct("2017-01-02 20:00:00"), by="min")),
  value = rep(1, 2101)))

I would like to include only 10:00:00 - 18:35:00 for each day, but can't achieve this nicely. My solution for now has been creating extra indic columns and then filter by them, but it hasn't worked well either.


Solution

  • You can use the function between() from data.table

    example[data.table::between(format(example$date, "%H:%M:%S"), 
                                lower = "10:00:00",
                                upper = "18:35:00"), ]