R 中的 15 分钟时间聚合 [英] 15min time aggregation in R

查看:30
本文介绍了R 中的 15 分钟时间聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个动物园系列.我可以在 chron 或 POSIXct 索引之间进行选择.

I have a zoo series in R. I can choose between a chron or a POSIXct index.

如何聚合到 15 分钟,每 15 分钟取最后一个元素?

How can I aggregate to 15min, taking the last element every 15min?

我知道如何每天汇总,写作日期,但不知道如何每 15 分钟汇总一次.

I know how to aggregate daily, writing as.Date, but not how to aggregate every 15min.

谢谢.

推荐答案

根据您的需要,这里有几种可能性.两者都使用 chron 包中的 trunc.times.aggregate.zoo 解决方案获取每 15 分钟间隔内的最后一个值,并使用 15 分钟间隔开始时的时间对其进行标记,因此使用的时间为:00:00:0000:15:0000:30:0000:45:00.duplicated 解决方案使用相同的值,但使用最后一次在数据中实际找到的时间标记它们.在这两种情况下,我们只包括存在数据的区间.

Here are a couple of possibilities depending on what you want. Both make use of trunc.times from the chron package. The aggregate.zoo solution takes the last value within each 15 minute interval and labels it using the time at the beginning of the 15 minute interval so the times used are: 00:00:00, 00:15:00, 00:30:00 and 00:45:00. The duplicated solution uses the same values but labels them using the last time actually found in the data. In both cases we only include intervals for which data is present.

在(1)?aggregate.zoo中有更多aggregate.zoo的例子,(2)所有三个动物园小插曲都有例子和(3)搜索词 aggregate.zootrunc 的 r-help 档案可以找到更多示例.

There are more examples of aggregate.zoo in (1) ?aggregate.zoo, (2) all three of the zoo vignettes have examples and (3) searching the r-help archives for the words aggregate.zoo and trunc finds even more examples.

library(zoo)
library(chron)
z <- zoo(1:10, chron(1:10/(24*13)))

# 1. last value in each 15 minute interval 
# using time at which interval begins

aggregate(z, trunc(time(z), "00:15:00"), tail, 1)

# 2. last value in each 15 minute interval 
# time of last point in data within interval

z[!duplicated(trunc(time(z), "00:15:00"), fromLast = TRUE)]

这篇关于R 中的 15 分钟时间聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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