Konrad Bauer
Konrad Bauer

Reputation: 101

filter for specific time of day (timespan) from POSIXct with argument for multiple days

I know that I can use POSIXct in dplyr:filter as an argument. I have successfully shortened my data by using arguments like >=. Here is my code:

library (dplyr)
start <- as.POSIXct("2018-05-18 00:00")
tseq <- seq(from = start, length.out = 1440, by = "10 mins")
observations <- data.frame(
  Time = tseq,
  Temp = sample(10:37,1440, replace = TRUE, set.seed(seed = 10)),
  Variable1 = sample(1:200,1440, replace = TRUE, set.seed(seed = 187)),
  Variable2 = sample(300:800,1440, replace = TRUE, set.seed(seed = 333))
)

observations_short <- observations %>% filter (Time <=  as.POSIXct ("2018-05-23 00:00", tz="CET") )

I assumed that something like this should be working to filter the values for every day 9:00 to 17:00h, but I couldn´t figure out working syntax for that.

    observations_9to5 <- observations %>% filter (Time >=  as.POSIXct ("09:00", tz="CET") ) %>% filter (Time <=  as.POSIXct ("17:00", tz="CET") )

Help with the syntax, if this generally works with dplyr::filter will be greatly appreciated. If this doesn´t work with dplyr, is there a possibility to do it with xts formatted data?

Upvotes: 1

Views: 704

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

It is quite simple to do this with lubridate. We extract the hour component and compare it accordingly.

library(lubridate)
library(dplyr)

observations %>%
    filter(hour(Time) >= 9 & hour(Time) < 17)


#                  Time Temp Variable1 Variable2
#1   2018-05-18 09:00:00   15       113       782
#2   2018-05-18 09:10:00   26        30       379
#3   2018-05-18 09:20:00   22       136       630
#4   2018-05-18 09:30:00   23        49       781
#....

Or if you want to continue with as.POSIXct approach it would be a bit more complex and would be something like

observations %>%
     filter(as.POSIXct(format(Time, "%H:%M:%S"), format = "%H:%M:%S") >= 
                      as.POSIXct("09:00:00", format = "%H:%M:%S") &
            as.POSIXct(format(Time, "%H:%M:%S"), format = "%H:%M:%S") < 
                    as.POSIXct("17:00:00", format = "%H:%M:%S"))

Here, we extract the time component by using format and then convert it back to POSIXct format and then compare it with 9 and 17 hours.

Upvotes: 1

Related Questions