I have a bit of a question about computing the Rolling Mean/standard deviation based on conditions. To be honest it is more of a syntax question, but since I think it is slo
There now also is a rolling mean function within data.table itself, please see github disscussion for details. The implementation is really straightforward.
DT[, rollmean := data.table::frollmean(x, n = 3, fill = 0, align = "right"),
by = .(stock)]
A quick benchmarking of the two, shows that the data.table
version is a bit quicker (most of the time).
library(microbenchmark)
microbenchmark(a = DT[, rollmean := data.table::frollmean(x, n = 3, fill = 0, align = "right"),
by = .(stock)]
, b = DT[, rollmean := rollmean(x, k = 3, fill = 0, align = "right"),
by = .(stock)]
, times = 100L
)
Unit: milliseconds
expr min lq mean median uq max neval cld
a 1.5695 1.66605 2.329675 1.79340 2.1980 39.3750 100 a
b 2.6711 2.82105 3.660617 2.99725 4.3577 20.3178 100 b