How do I take a rolling product using data.table

Here are two ways.. albeit not the most efficient implementations possible:

require(data.table)
N = 3L
dt[, prod := prod(dt$x[.I:(.I+N-1L)]), by=1:nrow(dt)]

Another one using embed():

tmp = apply(embed(dt$x, N), 1, prod)
dt[seq_along(tmp), prod := tmp]

Benchmarks:

set.seed(1L)
dt = data.table(x=runif(1e6))
zoo_fun <- function(dt, N) {
    rollapply(dt$x, N, FUN=prod, fill=NA, align='left')
}

dt1_fun <- function(dt, N) {
    dt[, prod := prod(dt$x[.I:(.I+N-1L)]), by=1:nrow(dt)]
    dt$prod
}

dt2_fun <- function(dt, N) {
    tmp = apply(embed(dt$x, N), 1L, prod)
    tmp[1:nrow(dt)]
}

david_fun <- function(dt, N) {
    Reduce(`*`, shift(dt$x, 0:(N-1L), type="lead"))
}

system.time(ans1 <- zoo_fun(dt, 3L))
#    user  system elapsed 
#   8.879   0.264   9.221 
system.time(ans2 <- dt1_fun(dt, 3L))
#    user  system elapsed 
#  10.660   0.133  10.959
system.time(ans3 <- dt2_fun(dt, 3L))
#    user  system elapsed 
#   1.725   0.058   1.819 
system.time(ans4 <- david_fun(dt, 3L))
#    user  system elapsed 
#   0.009   0.002   0.011 

all.equal(ans1, ans2) # [1] TRUE
all.equal(ans1, ans3) # [1] TRUE
all.equal(ans1, ans4) # [1] TRUE

Here's another possible version using data.table::shift combined with Reduce (as per @Aruns comment)

library(data.table) #v1.9.6+
N <- 3L
dt[, Prod3 := Reduce(`*`, shift(x, 0L:(N - 1L), type = "lead"))]

shift is vectorized, meaning it can create several new columns at once depending on the vector passed to the n argument. Then, Reduce is basically applies * to all the vectors at once element-wise.


you can try

library(zoo)
rollapply(dt, 3, FUN = prod)
          x
[1,] 0.7200
[2,] 0.5400
[3,] 0.3000
[4,] 0.0375

To match the expected output

dt[, Prod.3 :=rollapply(x, 3, FUN=prod, fill=NA, align='left')]

Tags:

R

Data.Table