累积总和,移动平均数和SQL“分组依据" R中的等价物 [英] Cumulative sums, moving averages, and SQL "group by" equivalents in R
问题描述
在R中创建移动平均值或滚动总和的最有效方法是什么?滚动功能如何与分组依据"一起使用?
What's the most efficient way to create a moving average or rolling sum in R? How do you do the rolling function along with a "group by"?
推荐答案
虽然动物园很棒,但有时候有更简单的方法.如果数据表现良好且间隔均匀,则embed()函数可有效地使您创建时间序列的多个滞后版本.如果您在VARS包中查找向量自动回归,则会看到包作者选择了此路线.
While zoo is great, sometimes there are simpler ways. If you data behaves nicely, and is evenly spaced, the embed() function effectively lets you create multiple lagged version of a time series. If you look inside the VARS package for vector auto-regression, you will see that the package author chooses this route.
例如,要计算x的3个周期滚动平均值,其中x =(1-> 20)^ 2:
For example, to calculate the 3 period rolling average of x, where x = (1 -> 20)^2:
> x <- (1:20)^2
> embed (x, 3)
[,1] [,2] [,3]
[1,] 9 4 1
[2,] 16 9 4
[3,] 25 16 9
[4,] 36 25 16
[5,] 49 36 25
[6,] 64 49 36
[7,] 81 64 49
[8,] 100 81 64
[9,] 121 100 81
[10,] 144 121 100
[11,] 169 144 121
[12,] 196 169 144
[13,] 225 196 169
[14,] 256 225 196
[15,] 289 256 225
[16,] 324 289 256
[17,] 361 324 289
[18,] 400 361 324
> apply (embed (x, 3), 1, mean)
[1] 4.666667 9.666667 16.666667 25.666667 36.666667 49.666667
[7] 64.666667 81.666667 100.666667 121.666667 144.666667 169.666667
[13] 196.666667 225.666667 256.666667 289.666667 324.666667 361.666667
这篇关于累积总和,移动平均数和SQL“分组依据" R中的等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!