【发布时间】:2022-01-19 12:25:20
【问题描述】:
我有很大的时间序列(日期时间、值、实例),在可视化之前,我需要使用每个实例的每个时间间隔的最大值(在我的示例中为 15 分钟)聚合数据。
我没有在 R 中找到原生聚合函数,所以我使用 celling_data 和 cut 方法创建了 2 个自定义函数。看我的例子:
library(tidyverse)
library(lubridate)
agg_fun_1 <- function (data, aggregation_period = 900) {
agg_period <- paste(aggregation_period, "secs")
agg_data <- data %>%
group_by(across(-c(Value, datetime)),
datetime = as.POSIXct(cut(datetime, agg_period)) + aggregation_period) %>%
summarise (Value = max(Value) , .groups = "drop") %>%
mutate(Value = ifelse(is.infinite(Value), NA, Value))
return (agg_data)
}
agg_fun_2 <- function (data, aggregation_period = "15 mins") {
agg_data <- data %>%
group_by(across(-c(Value, datetime)), datetime = ceiling_date (datetime, aggregation_period))
suppressWarnings(
agg_data <- agg_data %>%
summarise(Value = max(Value, na.rm = F), .groups = "drop") %>%
mutate(Value = ifelse(is.infinite(Value), NA, Value))
)
return (agg_data)
}
set.seed(42)
example_data <- tibble()
for(i in 1:256) {
example_data <- rbind(example_data,
data.frame( Instance = rep(i,20002),
datetime = seq.POSIXt(as.POSIXct("2020-12-26 10:00:00"), as.POSIXct("2020-12-26 10:00:00") + 15*20001, "15 sec"),
Value = sample(0:1000, 20002, replace=TRUE)
)
)
}
gc()
start_time <- Sys.time()
agg_fun_1(example_data)
end_time <- Sys.time()
end_time - start_time
gc()
start_time <- Sys.time()
agg_fun_2(example_data)
end_time <- Sys.time()
end_time - start_time
- agg_fun_1 执行时间为 2.3 分钟,RAM 使用量 - 在我的笔记本电脑上约为 +702 MB。
- agg_fun_2 执行时间为 1.9 分钟,RAM 使用量 - 在我的笔记本电脑上约为 +930 MB。
在真实环境中,我将并行运行 8 个 R 脚本,我的数据可能比我使用的数据大 3-5 倍。在这种情况下,我可能会面临资源不足的情况。
有什么方法可以优化我的函数的 RAM 使用和执行时间,或者有更好的聚合函数吗?
【问题讨论】:
标签: r optimization time-series aggregate