.SD in grouped operations causes update() to refit on wrong data subset
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start by running the reproducible data.table and lme4 example in the issue, then inspect how .SD is stored in the model formula environments during grouped and ungrouped operations. Compare the environments for m1 and m2 and verify that update() refits each model on its original subset, including the expected group-specific data.
Written by the indexing model from the issue text.
Description
I often fit models to subsets within data.tables, similar to Section 3.3 in the .SD vignette. However, is the following behavior intended?
A model fit m1 on the subset Grp == "A" is identical to the respective grouped model fit m2, as expected. Yet, a trivial update() call yields vastly different results, which did surprise me a little.
library(data.table)
library(lme4)
set.seed(2026)
dat <- CJ(Grp = c("A", "B"),
ID = factor(1:8),
Time = factor(c("T1", "T2", "T3", "T4")))
dat[, y := 3 * (Time == "T2") + rnorm(20, 3)[as.integer(ID)] + rnorm(.N, sd = 3)]
# Fit models using .SD --- ungrouped subset vs. grouped
m1 <- dat[Grp == "A", .(model = .(lmer(y ~ Time + (1 | ID))))]$model[[1]]
m2 <- dat[, .(model = .(lmer(y ~ Time + (1 | ID)))), by = Grp]$model[[1]]
all.equal(m1, m2) # equal as expected
all.equal(update(m1), update(m2)) # different results
As far as I understand, this issue stems from how environments are evaluated:
sd_m1 <- get(".SD", environment(m1@call$formula))
sd_m2 <- get(".SD", environment(m2@call$formula))
all.equal(sd_m1[, y], dat[Grp == "A", y]) # TRUE
all.equal(sd_m2[, y], dat[Grp == "B", y]) # TRUE, but should be Grp == "A"!
I would greatly appreciate it if there were (or someone could point me to) an elegant way to ensure that .SD retains the correct group-specific subset when used with a grouping variable :)
> sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────
setting value
version R version 4.6.0 (2026-04-24 ucrt)
os Windows 11 x64 (build 26100)
system x86_64, mingw32
ui Positron
language (EN)
collate German_Germany.utf8
ctype German_Germany.utf8
tz Europe/Berlin
date 2026-07-23
pandoc NA
─ Packages ────────────────────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
boot 1.3-32 2025-08-29 [2] CRAN (R 4.6.0)
cli 3.6.6 2026-04-09 [1] CRAN (R 4.6.0)
data.table * 1.18.4 2026-05-06 [1] CRAN (R 4.6.1)
lattice 0.22-9 2026-02-09 [2] CRAN (R 4.6.0)
lme4 * 2.0-1 2026-03-05 [1] CRAN (R 4.6.1)
MASS 7.3-65 2025-02-28 [2] CRAN (R 4.6.0)
Matrix * 1.7-5 2026-03-21 [2] CRAN (R 4.6.0)
minqa 1.2.8 2024-08-17 [1] CRAN (R 4.6.1)
nlme 3.1-169 2026-03-27 [2] CRAN (R 4.6.0)
nloptr 2.2.1 2025-03-17 [1] CRAN (R 4.6.1)
rbibutils 2.4.1 2026-01-21 [1] CRAN (R 4.6.1)
Rcpp 1.1.1-1.1 2026-04-24 [1] CRAN (R 4.6.0)
Rdpack 2.6.6 2026-02-08 [1] CRAN (R 4.6.1)
reformulas 0.4.4 2026-02-02 [1] CRAN (R 4.6.1)
rlang 1.2.0 2026-04-06 [1] CRAN (R 4.6.0)
sessioninfo 1.2.4 2026-06-04 [1] CRAN (R 4.6.0)
- Dominant language
- R
- Stars
- 3.9k
- Forks
- 1.1k
- Avg merge
- 14h 4m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·