Constructing moving average over a categorical variable in R

前端 未结 2 1332
小鲜肉
小鲜肉 2021-01-03 10:55

I\'m looking to construct a moving average while aggregating a timeseries dataset over two categorical variables. While I\'ve seen a few other tutorials, none of them seem t

2条回答
  •  情歌与酒
    2021-01-03 11:32

    ave and filter:

    with(df, ave(DV, Group, FUN=function(x) filter(x,rep(1/5,5),sides=1)))
    # [1]     NA     NA     NA     NA 4078.2 4535.8 4838.0 4901.6 4815.4 4735.4
    #[11] 4669.6 4682.0     NA     NA     NA     NA 4078.2 4535.8 4838.0 4901.6
    #[21] 4815.4 4735.4 4669.6 4682.0
    

提交回复
热议问题