gpt4 book ai didi

r - 如何将 30 个粒度数据聚合为 5 分钟数据

转载 作者:行者123 更新时间:2023-12-01 10:54:23 25 4
gpt4 key购买 nike

我有一个 30 秒的 CPU 数据,如下所示。我想做的是将这些数据聚合成 5 分钟和 10 分钟的平均值。

 dput(head(res,50))
structure(list(DATE = structure(c(1362114023, 1362114053, 1362114083,
1362114113, 1362114143, 1362114150, 1362114173, 1362114180, 1362114203,
1362114210, 1362114233, 1362114240, 1362114263, 1362114270, 1362114293,
1362114300, 1362114330, 1362114360, 1362114390, 1362114420, 1362114450,
1362114480, 1362114510, 1362114540, 1362114570, 1362114600, 1362114630,
1362114660, 1362114690, 1362114720, 1362114750, 1362114780, 1362114810,
1362114840, 1362114870, 1362114900, 1362114930, 1362114960, 1362114990,
1362115020, 1362115050, 1362115080, 1362115111, 1362115141, 1362115171,
1362115201, 1362115231, 1362115261, 1362115291, 1362115321), class = c("POSIXct",
"POSIXt"), tzone = ""), CPU = c(30L, 29L, 28L, 29L, 27L, 10L,
25L, 11L, 23L, 9L, 22L, 8L, 22L, 7L, 19L, 7L, 7L, 8L, 6L, 7L,
6L, 7L, 8L, 8L, 7L, 6L, 8L, 8L, 9L, 8L, 9L, 10L, 9L, 8L, 8L,
6L, 8L, 7L, 9L, 10L, 11L, 11L, 9L, 9L, 8L, 9L, 11L, 8L, 6L, 8L
)), .Names = c("DATE", "CPU"), row.names = c(132611L, 132612L,
132613L, 132614L, 132615L, 131428L, 132616L, 131429L, 132617L,
131430L, 132618L, 131431L, 132619L, 131432L, 132620L, 131433L,
131434L, 131435L, 131436L, 131437L, 131438L, 131439L, 131440L,
131441L, 131442L, 131443L, 131444L, 131445L, 131446L, 131447L,
131448L, 131449L, 131450L, 131451L, 131452L, 131453L, 131454L,
131455L, 131456L, 131457L, 131458L, 131459L, 131460L, 131461L,
131462L, 131463L, 131464L, 131465L, 131466L, 131467L), class = "data.frame")

任何想法,我可以如何处理我的颗粒数据?

最佳答案

Versions of this question have been asked and answered a bunch of times on stackoverflow. 然而它一直被问到。希望以下答案能够满足大多数人的需求:

首先,使用处理不规则时间序列的包。它使它变得容易得多。我喜欢 xts

library(xts)

mydata <- structure(list(DATE = structure(c(1362114023, 1362114053, 1362114083,
1362114113, 1362114143, 1362114150, 1362114173, 1362114180, 1362114203,
1362114210, 1362114233, 1362114240, 1362114263, 1362114270, 1362114293,
1362114300, 1362114330, 1362114360, 1362114390, 1362114420, 1362114450,
1362114480, 1362114510, 1362114540, 1362114570, 1362114600, 1362114630,
1362114660, 1362114690, 1362114720, 1362114750, 1362114780, 1362114810,
1362114840, 1362114870, 1362114900, 1362114930, 1362114960, 1362114990,
1362115020, 1362115050, 1362115080, 1362115111, 1362115141, 1362115171,
1362115201, 1362115231, 1362115261, 1362115291, 1362115321), class = c("POSIXct",
"POSIXt"), tzone = ""), CPU = c(30L, 29L, 28L, 29L, 27L, 10L,
25L, 11L, 23L, 9L, 22L, 8L, 22L, 7L, 19L, 7L, 7L, 8L, 6L, 7L,
6L, 7L, 8L, 8L, 7L, 6L, 8L, 8L, 9L, 8L, 9L, 10L, 9L, 8L, 8L,
6L, 8L, 7L, 9L, 10L, 11L, 11L, 9L, 9L, 8L, 9L, 11L, 8L, 6L, 8L
)), .Names = c("DATE", "CPU"), row.names = c(132611L, 132612L,
132613L, 132614L, 132615L, 131428L, 132616L, 131429L, 132617L,
131430L, 132618L, 131431L, 132619L, 131432L, 132620L, 131433L,
131434L, 131435L, 131436L, 131437L, 131438L, 131439L, 131440L,
131441L, 131442L, 131443L, 131444L, 131445L, 131446L, 131447L,
131448L, 131449L, 131450L, 131451L, 131452L, 131453L, 131454L,
131455L, 131456L, 131457L, 131458L, 131459L, 131460L, 131461L,
131462L, 131463L, 131464L, 131465L, 131466L, 131467L), class = "data.frame")

mydata.xts <- xts(mydata$CPU, order.by = mydata$DATE)

然后,调整 period.apply 基础架构,以便轻松地即时聚合到不同的窗口:

apply.periodly <- function (x, FUN, period, k=1, ...) 
{
if (!require("xts")) {
stop("Need 'xts'")
}
ep <- endpoints(x, on=period, k=k)
period.apply(x, ep, FUN, ...)
}

现在,创建您的聚合。

mydata.10m <- apply.periodly(x = mydata.xts, FUN = mean, period = "minutes", k = 10)
mydata.5m <- apply.periodly(x = mydata.xts, FUN = mean, period = "minutes", k = 5)

请注意,输出时间戳将反射(reflect)每个聚合窗口中的最后输入时间戳。

mydata.10m
[,1]
2013-03-01 00:09:30 14.80
2013-03-01 00:19:31 8.55
2013-03-01 00:22:01 8.40

mydata.5m
[,1]
2013-03-01 00:04:53 19.93333
2013-03-01 00:09:30 7.10000
2013-03-01 00:14:30 8.30000
2013-03-01 00:19:31 8.80000
2013-03-01 00:22:01 8.40000

但是,您可以将时间戳向上或向下舍入:

align.time.down=function(x,n){index(x)=index(x)-n;align.time(x,n)}

mydata.10m <- align.time(mydata.10m, 10*60)
mydata.10m
# [,1]
# 2013-03-01 00:10:00 14.80
# 2013-03-01 00:20:00 8.55
# 2013-03-01 00:30:00 8.40

mydata.5m <- align.time.down(mydata.5m, 5*60)
mydata.5m
# [,1]
# 2013-03-01 00:00:00 19.93333
# 2013-03-01 00:05:00 7.10000
# 2013-03-01 00:10:00 8.30000
# 2013-03-01 00:15:00 8.80000
# 2013-03-01 00:20:00 8.40000

关于r - 如何将 30 个粒度数据聚合为 5 分钟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16176240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com