I have a list of records of cars per hour that I summarise per day. I would like also for each day to record the hour of the first and last record.
I am trying this:
data_per_day <- data_per_hour %>%
group_by(date_loc = lubridate::date(date_loc)) %>%
summarise(sum_cars = sum(car),
count_start = min(hour(date_loc)),
count_last = max(hour(date_loc)),
year_day = yday(first(date_loc)))
However, count_start
and count_last
are always 0.
What am I doing wrong?
The following solution worked:
data_per_day <- data_per_hour %>%
group_by(date_loc_dt = lubridate::date(date_loc)) %>%
summarise(sum_cars = sum(car),
count_start = min(hour(date_loc)),
count_last = max(hour(date_loc)),
year_day = yday(first(date_loc)))
The previous code was overwriting date_loc
. Using the new variable date_loc_dt
solved the issue.
Thank you to @JonSpring for confirmation.