计算所有列中 3 个月或 6 个月移动平均值而不是 R 中的循环的有效方法

问题描述 投票:0回答:1

我想知道是否有一种更快的方法来计算未来 9 个月数据框中每一列的 3 个月或 6 个月的移动平均值。

我目前有大约 7k 列,所以我当前的方法是使用 for 循环来计算每一列,正如您可以假设的那样,这非常耗时。

我想知道是否还有其他替代方法?是否有类似并行处理或类似的东西可以获得所需的输出。

我还是 R 初学者,不确定是否有。我绝对会感激任何其他替代方法以及我如何能够对它们进行编码?

我正在使用的数据框如下:

structure(list(`Row Labels` = c("2019-01-01", "2019-02-01", "2019-03-01", 
"2019-04-01", "2019-05-01", "2019-06-01", "2019-07-01", "2019-08-01", 
"2019-09-01", "2019-10-01", "2019-11-01", "2019-12-01", "2020-01-01", 
"2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01", "2020-06-01", 
"2020-07-01", "2020-08-01", "2020-09-01", "2020-10-01", "2020-11-01", 
"2020-12-01", "2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01", 
"2021-05-01", "2021-06-01", "2021-07-01", "2021-08-01", "2021-09-01", 
"2021-10-01", "2021-11-01", "2021-12-01", "2022-01-01", "2022-02-01", 
"2022-03-01", "2022-04-01", "2022-05-01", "2022-06-01", "2022-07-01", 
"2022-08-01", "2022-09-01", "2022-10-01"), `XYZ|703` = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1636, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), `XYZ|851` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 206, 1814, 
2324, 772, 1116, 1636, 1906, 957, 829, 911, 786, 938, 1313, 2384, 
1554, 1777, 1635, 1534, 1015, 827, 982, 685, 767, 511, 239, 5400, 
1301, 426, 261, 201, 33, 0, 0, 0, 0, 0, 0), `XYZ|574` = c(0, 
0, 0, 0, 0, 0, 0, 0, 74, 179, 464, 880, 324, 184, 90, 170, 140, 
96, 78, 83, 83, 121, 245, 9000, 332, 123, 117, 138, 20, 42, 70, 
70, 42, 103, 490, 7500, 488, 245, 142, 95, 63, 343, 57, 113, 
100, 105), `XYZ|300` = c(82, 67, 140, 118, 24, 23, 93, 27, 63, 
62, 76, 50625, 56, 65, 55, 136, 99, 53, 5, 137, 111, 126, 208, 
50625, 61, 42, 225, 206, 69, 116, 82, 59, 51, 30, 63, 50625, 
40, 39, 96, 92, 101, 77, 43, 0, 0, 0), `XYZ|513` = c(74, 33, 
21, 31, 31, 29, 41, 21, 25, 60, 66, 5476, 47, 25, 23, 17, 34, 
0, 0, 0, 0, 0, 0, 5476, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5476, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `XYZ|204` = c(13, 41, 65, 107, 
42, 32, 79, 79, 44, 84, 79, 23409, 79, 36, 30, 51, 48, 51, 25, 
39, 15, 21, 65, 23409, 48, 40, 130, 94, 90, 92, 41, 59, 35, 4, 
83, 23409, 73, 38, 39, 28, 60, 71, 17, 51, 37, 26), `XYZ|895` = c(2, 
7, 26, 22, 10, 28, 13, 23, 18, 19, 30, 2209, 13, 6, 24, 20, 10, 
14, 23, 6, 21, 16, 32, 2209, 14, 14, 21, 27, 23, 45, 8, 12, 15, 
8, 29, 2209, 6, 1, 8, 10, 7, 2, 9, 8, 6, 13), `XYZ|666` = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22500, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 150, 22500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22500, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), `XYZ|575` = c(0, 0, 179, 464, 880, 
324, 184, 0, 0, 0, 0, 774400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
774400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 490, 774400, 488, 245, 
142, 0, 0, 0, 0, 0, 0, 0), `XYZ|345` = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -46L))

我当前使用的代码如下:

Book7 <- read_excel("C:/X/X/X- X/X/Book7.xlsx", 
                   sheet = "Loop")

Output = data.frame(matrix(NA,nrow = 9,ncol = ncol(Book7)))
colnames(Output) <- colnames(Book7)


for(i in 2:ncol(Book7))
{
  Y<- ts(data = Book7[,i],
         frequency = 12,
         start = c(2019,1),
         end = c(2022,10))
  
  Print(Y)
  
  mov_avg <- ma(Y, order = 3, centre = FALSE)
  
  Forecast_Mov_Avg_3M <- forecast(mov_avg,h = 19)
  
  print(Forecast_Mov_Avg_3M$mean)
  
  Output[,i] = as.data.frame(Forecast_Mov_Avg_3M$mean)
  
}
 
r dataframe forecasting
1个回答
0
投票

您可以将 for 循环的主体编写为函数,然后将 purrr::map 与该函数结合使用,例如:

 do_something <- function(x){
   Y <- ts(data = x,
           frequency = 12,
           start = c(2019,1),
           end = c(2022,10))
   
   mov_avg <- ma(Y, order = 3, centre = FALSE)     
   forecast(mov_avg,h = 19)}

 Output <- purrr::map(Book7, do_something)

map 将函数应用于列表 Book7 的每个条目,并返回一个列表,其中包含函数的每个返回值。

© www.soinside.com 2019 - 2024. All rights reserved.