将动物园时间序列截断为最接近的 10 分钟

发布于 2024-12-27 08:12:51 字数 1141 浏览 1 评论 0原文

我有一个动物园时间序列如下:

>> head(ww)
(11/22/08 11:37:00) (11/22/08 12:07:00) (11/22/08 12:22:00) (11/22/08 12:37:00) 
           0.087114            0.055422            0.055250            0.059483 
(11/22/08 12:52:00) (11/22/08 13:07:00) 
           0.057896            0.061808

如您所见,时间为 11:37、12:07、12:22 等。我想将这些时间更改为最接近的十分钟的整数倍 - 例如 XX: 10、XX:20。在这种情况下,11:37 将变为 11:40,12:07 将变为 12:10,12:22 将变为 12:20。

我已经找到了如何将其聚合到最接近的分钟:

wholemin <- function(x) trunc(x, units="minutes")
result = aggregate(r, wholemin, head, 1)

但是我不能使用 trunc 来聚合除秒、分钟、小时等以外的任何内容。

我应该如何去做呢?

为了便于示例,dput(head(ww) 的输出如下:

structure(c(0.087114, 0.055422, 0.05525, 0.059483, 0.057896, 
0.061808), index = structure(c(14205.4840277778, 14205.5048611111, 
14205.5152777778, 14205.5256944444, 14205.5361111111, 14205.5465277778
), format = structure(c("m/d/y", "h:m:s"), .Names = c("dates", 
"times")), origin = structure(c(1, 1, 1970), .Names = c("month", 
"day", "year")), class = c("chron", "dates", "times")), class = "zoo")

I have a zoo time series as follows:

>> head(ww)
(11/22/08 11:37:00) (11/22/08 12:07:00) (11/22/08 12:22:00) (11/22/08 12:37:00) 
           0.087114            0.055422            0.055250            0.059483 
(11/22/08 12:52:00) (11/22/08 13:07:00) 
           0.057896            0.061808

As you can see, the times are 11:37, 12:07, 12:22 etc. I would like to change these times to the nearest whole multiple of ten minutes - for example XX:10, XX:20. In this case 11:37 would become 11:40, 12:07 would become 12:10 and 12:22 would become 12:20.

I have found the how to aggregate it to the nearest minute:

wholemin <- function(x) trunc(x, units="minutes")
result = aggregate(r, wholemin, head, 1)

But I can't use trunc to aggregate to anything other than seconds, minutes, hours etc.

How should I go about doing this?

For ease of examples, the output of dput(head(ww) is below:

structure(c(0.087114, 0.055422, 0.05525, 0.059483, 0.057896, 
0.061808), index = structure(c(14205.4840277778, 14205.5048611111, 
14205.5152777778, 14205.5256944444, 14205.5361111111, 14205.5465277778
), format = structure(c("m/d/y", "h:m:s"), .Names = c("dates", 
"times")), origin = structure(c(1, 1, 1970), .Names = c("month", 
"day", "year")), class = c("chron", "dates", "times")), class = "zoo")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错爱 2025-01-03 08:12:51

试试这个:

to10 <- function(tt) trunc(tt + as.numeric(times("00:05:00")), units = "00:10:00")
aggregate(z, to10, mean)

并查看 ?trunc.times?aggregate.zoo 中的示例。

Try this:

to10 <- function(tt) trunc(tt + as.numeric(times("00:05:00")), units = "00:10:00")
aggregate(z, to10, mean)

and see the examples in ?trunc.times and ?aggregate.zoo .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文