How to convert minute data into hourly average data

I have one week of data reading every 5 seconds. The following is sample data.

9/1/2012 00:00:00    1
9/1/2012 00:00:05    2
9/1/2012 00:00:10    3

I want to calculate the hourly average for each day. Then make a multi-line graph of "average hourly reading versus hour" with lines representing different dates.

The one I have is for a weekly average

data$date = as.POSIXct(strptime(data$date, 
                  format = "%d/%m/%Y %H:%M","GMT")) 
means <- aggregate(data["nox"], format(data["date"],"%Y-%U"),
                 mean, na.rm = TRUE) 

For daily average

data$date = as.POSIXct(strptime(data$date, 
                 format = "%d/%m/%Y %H:%M","GMT"))
means <- aggregate(data["nox"], format(data["date"],"%Y-%j"),
                 mean, na.rm = TRUE) 

Anyone knows how to calculate the hourly average for each day.

+5
source share
3 answers

I like the @DWin answer, but I also remembered seeing a help file for ?cut.Date, which can also be used in this case. I compiled some data so you can see the results for several hours:

set.seed(1)
data <- data.frame(date = seq(from = ISOdatetime(2012, 01, 01, 00, 00, 00),
                              length.out = 4320, by=5),
                   nox = sample(1:20, 4320, replace=TRUE))

hr.means <- aggregate(data["nox"], 
                      list(hour = cut(data$date, breaks="hour")), 
                      mean, na.rm = TRUE)
hr.means
#                  hour      nox
# 1 2012-01-01 00:00:00 10.60694
# 2 2012-01-01 01:00:00 10.13194
# 3 2012-01-01 02:00:00 10.33333
# 4 2012-01-01 03:00:00 10.38194
# 5 2012-01-01 04:00:00 10.51111
# 6 2012-01-01 05:00:00 10.26944
+8

:

hr.means <- aggregate(dat["V1"], format(dat["date"],"%Y-%m-%d %H"),
             mean, na.rm = TRUE) 
hr.means
#---------
           date V2
1 2012-01-09 00  2
+5

I got it here , so I have the data in a slightly different form, but with lubridate you can also easily parse your data format.

library(tibble)
library(dplyr)
library(lubridate)

tbl <- tribble(
    ~TIME,                 ~MEASURE,
    "2018-01-01 06:58:50",    05,
    "2018-01-01 07:00:00",    10,
    "2018-01-01 07:04:45",    20,
    "2018-01-01 07:04:55",    25,
    "2018-01-01 07:21:00",    20,
    "2018-01-01 07:58:04",    18,
    "2018-01-01 07:59:59",    12,
    "2018-01-01 08:00:00",    17,
    "2018-01-01 08:01:04",    30
) %>% mutate(TIME = ymd_hms(TIME))

With data in a form in which you can manipulate the date / time, you can sum it up per day + hour or only per hour for all dates:

# if you want per date
tbl %>% 
    mutate(date = date(TIME), hour = hour(TIME)) %>% 
    group_by(date, hour) %>% summarise(m = mean(MEASURE))

# if you want per hour over all dates
tbl %>% 
    mutate(hour = hour(TIME)) %>% 
    group_by(hour) %>% summarise(m = mean(MEASURE))

To build it using dots and lines using ggplot2, you can do

library(ggplot2)
tbl %>% 
    mutate(hour = hour(TIME)) %>% 
    group_by(hour) %>% summarise(m = mean(MEASURE)) %>%
    ggplot(aes(x = hour, y = m)) + geom_point() + geom_line()
0
source

All Articles