Unexpected .GRP behaviour during update-by-reference with filter, while referring to the same data.table in j

Open
#7,367 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the minimal reproducible example using the grouped update-by-reference expression with .GRP and X[!is.na(val), max(val)]. Trace the .GRP and update-by-reference behavior involved, then confirm the case produces the expected values 2, 3, and 4 and add a regression test for the demonstrated behavior.

Written by the indexing model from the issue text.

Description

I understand that .GRP should produce a sequence of integers that are a group counter, where group 1 is 1, group 2 is 2, etc.

When doing an update-by-reference with filter, while referring to the data.table in j, I get unexpected behaviour.

Minimal reproducible example

library(data.table)

X <- data.table(
  grp1 = c("a", "a", "a", "b")
  , grp2 = c("c", "c", "d", "d")
)
X[, .GRP, grp1] # as expected
X[, .GRP, grp2] # as expected
X[, .GRP, .(grp1, grp2)] # as expected

X[, val := .GRP, .(grp1, grp2)] 
X # as expected

# reset the scenario
X[, val := NULL]

# this is a simplified example of my actual application
# the `val` column has been partially initialised, and now I need 
# to set the rest of the val column
X[1, val := 1]
X[is.na(val)
  , val := .GRP + X[!is.na(val), max(val)]
  , .(grp1, grp2)] 
X # unexpected values of .GRP. Would expect 2,3,4, but get 2,4,7

# reset the scenario
X[, val := NULL]
X[1, val := 1] 
X[is.na(val)
  , val := .GRP + 1
  , .(grp1, grp2)]
X # expected values. 

So it is something to do with X[!is.na(val), max(val)] code in the j component?

Output of sessionInfo()

> sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: x86_64-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-x86_64/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: Australia/Melbourne
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.17.8

loaded via a namespace (and not attached):
 [1] compiler_4.5.1    assertthat_0.2.1  rprojroot_2.1.0   tools_4.5.1       secret_1.1.0      rstudioapi_0.17.1 curl_7.0.0       
 [8] jsonlite_2.0.0    openssl_2.3.3     askpass_1.2.1 
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.