Using a variable to control the lag (or lead) shift fails when in a group:
library(data.table)
library(dplyr)
library(dtplyr)
LAG_ROWS <- 2L
data.table(g = rep(0,4), a = 1:4) |>
lazy_dt() |>
mutate(b = lag(a, n = LAG_ROWS))
# Source: local data table [4x3]
# Call: copy(`_DT1`)[, `:=`(b = shift(a, n = ..LAG_ROWS, type = "lag"))]
#
# g a b
# <dbl> <int> <int>
# 1 0 1 NA
# 2 0 2 NA
# 3 0 3 1
# 4 0 4 2
data.table(g = rep(0,4), a = 1:4) |>
lazy_dt() |>
group_by(g) |>
mutate(b = lag(a, n = LAG_ROWS))
# Error in eval(q[[i]], parent.frame(2L)) : object '..LAG_ROWS' not found
I think I understand what's happening in as far as dtplyr is translating to
data.table(g = rep(0,4), a = 1:4)[, `:=`(b = shift(a, n = ..LAG_ROWS, type = "lag")), by = .(g)]
# Error in eval(q[[i]], parent.frame(2L)) : object '..LAG_ROWS' not found
But if I drop the .. it works:
data.table(g = rep(0,4), a = 1:4)[, `:=`(b = shift(a, n = LAG_ROWS, type = "lag")), by = .(g)] |> print()
# g a b
# <num> <int> <int>
# 1: 0 1 NA
# 2: 0 2 NA
# 3: 0 3 1
# 4: 0 4 2
I can work around it by using the injection operator, but obviously it would be nice to have it working without that. :)
data.table(g = rep(0,4), a = 1:4) |>
lazy_dt() |>
group_by(g) |>
mutate(b = lag(a, n = !!LAG_ROWS))
# Source: local data table [4 x 3]
# Groups: g
# Call: copy(`_DT3`)[, `:=`(b = shift(a, n = 2L, type = "lag")), by = .(g)]
#
# g a b
# <dbl> <int> <int>
# 1 0 1 NA
# 2 0 2 NA
# 3 0 3 1
# 4 0 4 2
sessionInfo()
# R version 4.5.1 (2025-06-13)
# Platform: aarch64-apple-darwin20
# Running under: macOS Sequoia 15.6.1
#
# Matrix products: default
# BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# time zone: Pacific/Auckland
# tzcode source: internal
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] dplyr_1.1.4 dtplyr_1.3.2 data.table_1.17.8
#
# loaded via a namespace (and not attached):
# [1] R6_2.6.1 tidyselect_1.2.1 magrittr_2.0.4 glue_1.8.0 tibble_3.3.0 pkgconfig_2.0.3 generics_0.1.4 lifecycle_1.0.4 cli_3.6.5 vctrs_0.6.5
# [11] withr_3.0.2 compiler_4.5.1 rstudioapi_0.17.1 tools_4.5.1 pillar_1.11.1 rlang_1.1.6
Using a variable to control the
lag(orlead) shift fails when in a group:I think I understand what's happening in as far as
dtplyris translating toBut if I drop the
..it works:I can work around it by using the injection operator, but obviously it would be nice to have it working without that. :)