Average-version of cumsum in data.table (cumulative mean)

Viewed 106

I have some data

library(data.table); set.seed(42)
dat <- data.table(t=1:6, group=rep(1:2,each=3), val=runif(6,0,1))
> dat
   t group       val
1: 1     1 0.9148060
2: 2     1 0.9370754
3: 3     1 0.2861395
4: 4     2 0.8304476
5: 5     2 0.6417455
6: 6     2 0.5190959

and I would like to calculate a rolling average of val within each group. I can get a rolling sum using

dat[, cumsum:=cumsum(val), by=group]
> dat
   t group       val    cumsum
1: 1     1 0.9148060 0.9148060
2: 2     1 0.9370754 1.8518815
3: 3     1 0.2861395 2.1380210
4: 4     2 0.8304476 0.8304476
5: 5     2 0.6417455 1.4721931
6: 6     2 0.5190959 1.9912891

and I basically would need to divide cumsum by the number of rows but I don't know how to do this. I suppose .N would keep dividing each value by 3.

1 Answers

We can use cummean

library(data.table)
library(dplyr)
dat[, new :=cummean(val), by=group]

Or if we need to divide use seq_len(.N)

dat[, new1 := cumsum(val)/seq_len(.N), by = group]
Related