简体   繁体   English

按组R data.table进行滚动平均

[英]Rolling average by group R data.table

I want to compute a YTD rolling average by group starting from the first row in the group and ending at the last row. 我想按组计算YTD滚动平均值,从组的第一行开始到最后一行结束。 Sample below... 以下示例......

Group <- c(rep("a",5), rep("b",5))
Sales <- c(2,4,3,3,5,9,7,8,10,11)
Result <- c(2,3,3,3,3.4,9,8,8,8.5,9)
df <- data.frame(Group, Sales, Result)

The Result column is what I am expecting to see from the rolling average. 结果列是我期望从滚动平均值中看到的。

Using cumsum : 使用cumsum

dt <- as.data.table(df)
dt[, res := cumsum(Sales)/(1:.N), by = Group]
dt
    Group Sales Result res
 1:     a     2    2.0 2.0
 2:     a     4    3.0 3.0
 3:     a     3    3.0 3.0
 4:     a     3    3.0 3.0
 5:     a     5    3.4 3.4
 6:     b     9    9.0 9.0
 7:     b     7    8.0 8.0
 8:     b     8    8.0 8.0
 9:     b    10    8.5 8.5
10:     b    11    9.0 9.0

or with rollapplyr from the zoo package: 或者来自zoo套餐的rollapplyr

dt[, res := rollapplyr(Sales, 1:.N, mean), by = Group]

or with base R: 或与基地R:

ave(df$Sales, df$Group, FUN = function(x) cumsum(x) / seq_along(x))

We can use dplyr with zoo . 我们可以在zoo使用dplyr The %>% connects the lhs with the rhs and it is very easy to understand and execute it. %>%将lhs与rhs连接起来,很容易理解和执行它。

library(dplyr)
library(zoo)
df %>%
   group_by(Group) %>% 
   mutate(Sales = rollapplyr(Sales, row_number(), mean))  
#    Group Sales Result
#    <fctr> <dbl>  <dbl>
#1       a   2.0    2.0
#2       a   3.0    3.0
#3       a   3.0    3.0
#4       a   3.0    3.0
#5       a   3.4    3.4
#6       b   9.0    9.0
#7       b   8.0    8.0
#8       b   8.0    8.0
#9       b   8.5    8.5
#10      b   9.0    9.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM