累积和、移动平均线和 SQL“分组依据"R中的等价物 [英] Cumulative sums, moving averages, and SQL "group by" equivalents in R

查看:12
本文介绍了累积和、移动平均线和 SQL“分组依据"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆