如何将分钟数据转换为小时平均数据 [英] How to convert by the minute data to hourly average data

查看:51
本文介绍了如何将分钟数据转换为小时平均数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一周的数据,每 5 秒读取一次.数据示例如下.

I have one week of data with a reading every 5 seconds. An example of data is below.

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 plot of "average hourly reading vs. hour" with lines representing different dates.

我这里有一个是每周平均

The one I have here is for 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) 

对于日均值

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) 

任何人都知道如何计算每天的每小时平均值.

Any one knows how to calculate the hourly average for each day.

推荐答案

我喜欢@DWin 的回答,但我也记得曾经看过一个 ?cut.Date 的帮助文件,它也可以使用在这种情况下.我整理了一些数据,以便您可以在几个小时内查看结果:

I like @DWin's answer, but I had also remembered seeing once a help file for ?cut.Date which can also be used in this case. I've made up some data so you can see the results over a few 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

这篇关于如何将分钟数据转换为小时平均数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆