Lisa Hlmsch
Lisa Hlmsch

Reputation: 11

How to calculate hourly averages per week

DateTime totalgen

I have a dataframe with roughly 200k rows, showing visitor figures over a timespan of many months. I would like to plot a graph per season (total 4 plots), showing the average number of visitors per 15 min during a week. Per plot,

head(dataset, 9)

            <DateTime>    <visitors>
 1 2014-12-01 00:00:00        12
 2 2014-12-01 00:15:00        2335
 3 2014-12-01 00:30:00        2366
 4 2014-12-01 00:45:00        12254
 5 2014-12-01 01:00:00        45
 6 2014-12-01 01:15:00        0
 7 2014-12-01 01:30:00        0
 8 2014-12-01 01:45:00        12
 9 2014-12-01 02:00:00        122

How do I calculate the average number of visitors for a 15 min timestamp?

Upvotes: 0

Views: 102

Answers (1)

CPak
CPak

Reputation: 13581

Using lubridate

library(lubridate)
ds <- dataset %>% 
        rowwise() %>%
        mutate(dummy = paste0(week(ymd_hms(DateTime)), hour(ymd_hms(DateTime)), minute(ymd_dms(DateTime)))) %>%
        ungroup() %>%
        group_by(dummy) %>%
        summarise(visitors=mean(visitors))

Upvotes: 2

Related Questions