Search code examples
rdplyrlubridate

group by day (not date) and hour of the day


How can I group by day (not date) and hour of the day and count readings? So I will have 7*24 groups.

library(lubridate)
library(dplyr)

readings <- ymd_hms(c("2018-01-05 12:00:15", "2018-01-01 02:00:15", "2018-02-25 12:00:15", 
                  "2018-04-15 11:00:15", "2020-10-15 10:00:15", "2019-10-20 08:00:15",
                  "2019-11-15 02:01:15", "2018-11-02 11:00:15", "2018-07-09 02:00:15",
                  "2020-10-02 01:00:15", "2020-01-29 02:00:15", "2019-03-15 07:00:15")
)

tbl <- tibble(readings)

Solution

  • it can be obtained in this way;

    tbl %>%
    mutate(day=wday(readings),hour=hour(readings)) %>%
    group_by(day,hour)