Search code examples
rdplyrlubridate

Remove time periods from daily data


I have a dataset of hourly observations with the format %Y%m%d %H:%M that results like this 2020-03-01 01:00:00 for various days. How can filter filter out a certain time interval? My goal is to maintain the observations between 08:00 and 20:00.


Solution

  • You can extract the hour value from the column and keep the rows between 8 and 20 hours.

    df$hour <- as.integer(format(df$datetime, '%H'))
    result <- subset(df, hour >= 8 & hour <= 20)
    result
    
    #               datetime hour
    #9   2020-01-01 08:00:00    8
    #10  2020-01-01 09:00:00    9
    #11  2020-01-01 10:00:00   10
    #12  2020-01-01 11:00:00   11
    #13  2020-01-01 12:00:00   12
    #14  2020-01-01 13:00:00   13
    #15  2020-01-01 14:00:00   14
    #16  2020-01-01 15:00:00   15
    #17  2020-01-01 16:00:00   16
    #18  2020-01-01 17:00:00   17
    #19  2020-01-01 18:00:00   18
    #20  2020-01-01 19:00:00   19
    #21  2020-01-01 20:00:00   20
    #33  2020-01-02 08:00:00    8
    #34  2020-01-02 09:00:00    9
    #35  2020-01-02 10:00:00   10
    #...
    #...
    

    data

    df <- data.frame(datetime = seq(as.POSIXct('2020-01-01 00:00:00', tz = 'UTC'), 
                          as.POSIXct('2020-01-10 00:00:00', tz = 'UTC'), 'hour'))