使用hourly.apply函数将10分钟数据聚合为每小时平均值 [英] Aggregating 10 minute data to hourly mean with the hourly.apply function fails

查看:210
本文介绍了使用hourly.apply函数将10分钟数据聚合为每小时平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期/时间数据及其表示日期和时间的测量值的文件。在一个月的过程中,每十分钟测量一次该值,我最终尝试进行时间序列分析。但是,在此之前,我想通过计算每60分钟的平均值来将10分钟间隔汇总为小时间隔。这是我的数据样本(总共4319个观测值):

I have a file with date/time data and its measured values for said date and time. The values were measured every ten minutes for the course of one month, and I am attempting to do a time series analysis eventually. Before that however, I wanted to aggregate the 10 minute intervals to hourly intervals by calculating the mean measurement of every 60 minutes. Here is a sample of my data(a total of 4319 observations):

Date/Time                 Value

2013-01-01 00:00:00     31,439999   
2013-01-01 00:10:00     33,439999   
2013-01-01 00:20:00     39         
2013-01-01 00:30:00     35,279999   
2013-01-01 00:40:00     37,279999   
2013-01-01 00:50:00     32,199997   
2013-01-01 01:00:00     35,279999   
2013-01-01 01:10:00     38,199997

我的日期/时间数据为POSIXlt类型,而所测得的值为类型因子。我在该网站上搜索了
,发现其他用户发布了几个主题,但它们并不都适用于我,或者我无法使用他们的帖子中给出的建议来重新创建相同的结果。

My date/time data is of the type POSIXlt and the values measured are of the type factor. I have searched on this site and I have found several topics posted by other users but they do not all apply to me, or I can not recreate the same results using suggestions given on their posts.

例如,另一个用户问了几乎与我相同的问题:将15分钟步长的值汇总为小时步长的值
,我完全按照他们的答案提供的步骤进行操作。

For example, another user asked almost the exact same question as me: Aggregate values of 15 minute steps to values of hourly steps and I followed exactly the steps that their answers provided.

library(xts)
dat.xts <- xts(data$values,
           as.POSIXct(data$datetime))
hourly.apply(dat.xts,mean)

但是对于最后一行,我得到以下错误消息:

but for the last line I get the following error message:

Error: could not find function "hourly.apply"

尽管我已经安装了xts软件包和zoo软件包,但hourly.apply函数似乎已停止从。这可能是什么原因?
预先谢谢您。

Although I did already install the xts package as well as the zoo package, which the hourly.apply function appears to stem from. What could be the reason for this? Thank you in advance.

推荐答案

hourly.apply似乎不存在,但查看 xp包中的apply.daily'函数似乎很容易创建。

"hourly.apply" doesn't seem to exist but looking at the 'apply.daily' function in the xts package it seems straightforward to create.

请参阅xts :: apply.daily。我将'days'更改为'hours'以产生以下内容

see xts::apply.daily. I've changed 'days' to 'hours' to produce the following

apply.hourly <- function(x, FUN,...) {
  ep <- endpoints(x, 'hours')
  period.apply(x, ep, FUN, ...)
}

尝试一下

my.time <- seq(from = as.POSIXct('2000-01-01 00:00:00'),
           to = as.POSIXct('2000-01-01 2:00:00'),
           by = '10 min')

my.data <- rep(10, length = length(my.time))
my.data <- as.xts(my.data, order.by = my.time)

apply.hourly(my.data, sum)

                [,1]
2000-01-01 00:50:00   60
2000-01-01 01:50:00   60
2000-01-01 02:00:00   10

这篇关于使用hourly.apply函数将10分钟数据聚合为每小时平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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