gpt4 book ai didi

r - 如何将 30 秒的数据聚合为 R 中的 15 分钟数据

转载 作者:行者123 更新时间:2023-12-04 04:59:34 25 4
gpt4 key购买 nike

我有来自一堆服务器的 30 秒粒度数据。我想为每个服务器将此数据滚动到 15 分钟。

我的数据框是这样的:

输出(p)

structure(list(DATE = c("2013-04-15   02:47:32", "2013-04-15   02:48:02", 
"2013-04-15 02:48:32", "2013-04-15 02:49:02", "2013-04-15 02:49:32",
"2013-04-15 02:50:02", "2013-04-15 02:50:32", "2013-04-15 02:51:02",
"2013-04-15 02:51:32", "2013-04-15 02:52:02", "2013-04-15 02:52:32",
"2013-04-15 02:53:02", "2013-04-15 02:53:32", "2013-04-15 02:54:02",
"2013-04-15 02:54:32", "2013-04-15 02:55:02", "2013-04-15 02:55:32",
"2013-04-15 02:56:02", "2013-04-15 02:56:32", "2013-04-15 02:57:02",
"2013-04-29 17:33:07", "2013-04-29 17:33:37", "2013-04-29 17:34:07",
"2013-04-29 17:34:37", "2013-04-29 17:35:07", "2013-04-29 17:35:37",
"2013-04-29 17:36:07", "2013-04-29 17:36:37", "2013-04-29 17:37:07",
"2013-04-29 17:37:37", "2013-04-29 17:38:07", "2013-04-29 17:38:37",
"2013-04-29 17:39:07", "2013-04-29 17:39:37", "2013-04-29 17:40:07",
"2013-04-29 17:40:37", "2013-04-29 17:41:07", "2013-04-29 17:41:37",
"2013-04-29 17:42:07", "2013-04-29 17:42:37"), Server = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("ServerA", "ServerB"), class = "factor"),
CPU = c(70L, 71L, 72L, 72L, 72L, 73L, 73L, 74L, 73L, 73L,
73L, 73L, 71L, 74L, 72L, 72L, 70L, 72L, 71L, 70L, 78L, 79L,
79L, 78L, 79L, 77L, 78L, 80L, 81L, 80L, 80L, 79L, 79L, 79L,
81L, 79L, 78L, 79L, 79L, 79L)), .Names = c("DATE", "Server",
"CPU"), class = "data.frame", row.names = c(NA, -40L))

是否有一种简单的方法可以将每个服务器的 30 秒数据滚动到 15 分钟数据?我可以在此数据框中拥有 2 个以上的服务器。

例如,如果我的数据如下,其中包括 30 秒的数据。我需要每 15 分钟平均一次 CPU 数据。
      DATE       SERVER CPU
1 2013-04-15 02:47:32 ServerA 70
2 2013-04-15 02:48:02 ServerA 71
3 2013-04-15 02:48:32 ServerA 72
4 2013-04-15 02:49:02 ServerA 72
5 2013-04-15 02:49:32 ServerA 72
6 2013-04-15 02:50:02 ServerA 73
:
:
:
:

最佳答案

首先,将您的 sring 转换为 POSIXct 类:

as.POSIXct(strptime("2013-04-15 02:47:32", "%Y-%m-%d %H:%M:%S"))

接下来,取消分类以获得纪元(自 1970-01-01 以来的秒数):
unclass(as.POSIXct(strptime("2013-04-15 02:47:32", "%Y-%m-%d %H:%M:%S")))

最后,截断过去 15 分钟间隔(15*60 秒)之后的秒数:
floor(unclass(as.POSIXct(strptime("2013-04-15 02:47:32", 
"%Y-%m-%d %H:%M:%S"))
) / (15*60)
) * (15*60)

全部放在数据框上:
as.POSIXct(floor(unclass(as.POSIXct(strptime("2013-04-15   02:47:32", "%Y-%m-%d %H:%M:%S")))/(15*60))*(15*60), origin='1970-01-01 00:00.00 UTC')

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

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