使用rollapply和动物园计算一列变量的滚动平均值 [英] use rollapply and zoo to calculate rolling average of a column of variables

查看:218
本文介绍了使用rollapply和动物园计算一列变量的滚动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算列sp中所有变量的滚动平均值。这是我的数据样本:

  the_date sp赢
01-06--2012 1 305
02-06--2012 1 276
03-06--2012 1 184
04-06--2012 1 248
05-06--2012 1 243
06 -06--2012 1 363
07-06--2012 1 272
01-06--2012 2 432
02-06--2012 2 369
03-06 --2012 2 302
04-06--2012 2 347
05-06--2012 2 357
06-06--2012 2 331
07-06-- 2012 2 380
01-06--2012 3 1
02-06--2012 3 2
03-06--2012 3 3
04-06--2012 3 2
05-06--2012 3 0
06-06--2012 3 2
07-06--2012 3 0

我想要的是将一列添加到数据中,为每个sp提供3天的移动平均值。所以下面的输出是我的愿望:

  the_date sp wins SMA_wins 
01-06--2012 1 305 305.00
02-06--2012 1 276 290.50
03-06--2012 1 184 255.00
04-06--2012 1 248 236.00
05-06--2012 1 243 225.00
06-06--2012 1 363 284.67
07-06--2012 1 272 292.67
01-06--2012 2 432 432.00
02-06-- 2012 2 369 400.50
03-06--2012 2 302 367.67
04-06--2012 2 347 339.33
05-06--2012 2 357 335.33
06-06 --2012 2 331 345.00
07-06--2012 2 380 356.00
01-06--2012 3 1 1.00
02-06--2012 3 2 1.50
03 -06--2012 3 3 2.00
04-06--2012 3 2 2.33
05-06--2012 3 0 1.67
06-06--2012 3 2 1.33
07-06--2012 3 0 0.67

我正在使用rollapply。

  df < -  group_by(df,sp)
df_zoo < - 动物园(df $ wins,df $ the_date)
mutate(df,SMA_wins = rollapplyr(df_zoo,3,mean,align =right,partial = TRUE))

如果我在特定的sp上过滤数据,它完美的工作。

当我按sp分组时,如何使这项工作成功?



谢谢

解决方案

您可以这样做:

 图书馆(dplyr)
图书馆(动物园)

df%>%group_by( sp)%>%
mutate(SMA_wins = rollapplyr(wins,3,mean,partial = TRUE))

它看起来像在 mutate中使用 df df_zoo 电话正在搞砸。


I want to calculate the rolling mean for all variables in column "sp". This is a sample of my data:

the_date    sp  wins
01-06--2012 1   305
02-06--2012 1   276
03-06--2012 1   184
04-06--2012 1   248
05-06--2012 1   243
06-06--2012 1   363
07-06--2012 1   272
01-06--2012 2   432
02-06--2012 2   369
03-06--2012 2   302
04-06--2012 2   347
05-06--2012 2   357
06-06--2012 2   331
07-06--2012 2   380
01-06--2012 3   1
02-06--2012 3   2
03-06--2012 3   3
04-06--2012 3   2
05-06--2012 3   0
06-06--2012 3   2
07-06--2012 3   0

What I want, is to have a column added to data, that gives the moving average over 3 days for each sp. So the following output is what I desire:

the_date    sp  wins    SMA_wins
01-06--2012 1   305     305.00
02-06--2012 1   276     290.50
03-06--2012 1   184     255.00
04-06--2012 1   248     236.00
05-06--2012 1   243     225.00
06-06--2012 1   363     284.67
07-06--2012 1   272     292.67
01-06--2012 2   432     432.00
02-06--2012 2   369     400.50
03-06--2012 2   302     367.67
04-06--2012 2   347     339.33
05-06--2012 2   357     335.33
06-06--2012 2   331     345.00
07-06--2012 2   380     356.00
01-06--2012 3   1       1.00
02-06--2012 3   2       1.50
03-06--2012 3   3       2.00
04-06--2012 3   2       2.33
05-06--2012 3   0       1.67
06-06--2012 3   2       1.33
07-06--2012 3   0       0.67

I am using rollapply.

df <- group_by(df, sp)
df_zoo <- zoo(df$wins, df$the_date) 
mutate(df, SMA_wins=rollapplyr(df_zoo, 3, mean,  align="right", partial=TRUE))

If I filter my data on a specific sp, it works perfectly.

How can I make this work when I group by sp?

Thanks

解决方案

You can do it like this:

library(dplyr)
library(zoo)

df %>% group_by(sp) %>%
       mutate(SMA_wins=rollapplyr(wins, 3, mean, partial=TRUE))

It looks like your use of df and df_zoo in your mutate call was messing things up.

这篇关于使用rollapply和动物园计算一列变量的滚动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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