Assigning some values to randomly selected rows leads to incorrect results

Open
#5,703 6 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
Clearly specified
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the reproducible R example and compare the inline row selection with the precomputed IDs case. Trace data.table's [] subset-assignment and index handling around the reported verbose output. Done means the random-row assignment updates only the selected ID and a regression test covers both forms.

Written by the indexing model from the issue text.

Description

bug

I needed to change the value in cells of rows selected at random but doing so gives incorrect results. There is no error, just the result is incorrect, hence it was particularly hard to track this down when dealing with thousands of row entries.

library(data.table)

options(datatable.verbose = TRUE)

set.seed(123)

dt <- data.table(
  ID = 1000 + c(1:10),
  Event = sample(c(T, F), 10, replace = T)
)

# choose an ID at random to change to FALSE
nToChange <- 1
dt[ID %in% sample(dt[Event == TRUE]$ID, nToChange)]$Event <- FALSE
dt

      ID Event
 1: 1001  TRUE
 2: 1002  TRUE
 3: 1003  TRUE
 4: 1004 FALSE
 5: 1005  TRUE
 6: 1006 FALSE
 7: 1007 FALSE
 8: 1008 FALSE
 9: 1009  TRUE
10: 1005 FALSE

As you can see above, the ID for 1010 has been changed to 1005!

Selecting the rows to change outside the [] gives correct results.


set.seed(123)

dt <- data.table(
  ID = 1000 + c(1:10),
  Event = sample(c(T, F), 10, replace = T)
)

# choose an ID at random to change to FALSE
nToChange <- 1
idsToChange <- sample(dt[Event == TRUE]$ID, nToChange)
dt[ID %in% idsToChange]$Event <- FALSE
dt

      ID Event
 1: 1001  TRUE
 2: 1002  TRUE
 3: 1003  TRUE
 4: 1004 FALSE
 5: 1005 FALSE
 6: 1006 FALSE
 7: 1007 FALSE
 8: 1008 FALSE
 9: 1009  TRUE
10: 1010  TRUE

The verbose output from the first data.table assignment is given below.

Creating new index 'Event'
Creating index Event done in ... forder.c received 10 rows and 2 columns
forder took 0 sec
0.040s elapsed (0.039s cpu) 
Optimized subsetting with index 'Event'
forder.c received 1 rows and 1 columns
forder took 0 sec
x is already ordered by these columns, no need to call reorder
i.Event has same type (logical) as x.Event. No coercion needed.
on= matches existing index, using index
Starting bmerge ...
bmerge done in 0.000s elapsed (0.000s cpu) 
Constructing irows for '!byjoin || nqbyjoin' ... 0.000s elapsed (0.000s cpu) 
Creating new index 'ID'
Creating index ID done in ... forder.c received 10 rows and 2 columns
forder took 0 sec
0.033s elapsed (0.032s cpu) 
Optimized subsetting with index 'ID'
forder.c received 1 rows and 1 columns
forder took 0 sec
x is already ordered by these columns, no need to call reorder
i.ID has same type (double) as x.ID. No coercion needed.
on= matches existing index, using index
Starting bmerge ...
bmerge done in 0.000s elapsed (0.000s cpu) 
Constructing irows for '!byjoin || nqbyjoin' ... 0.000s elapsed (0.000s cpu) 
Assigning to all 1 rows
RHS_list_of_columns == false
RHS for item 1 has been duplicated because NAMED==5 MAYBE_SHARED==1, but then is being plonked. length(values)==1; length(cols)==1)
Optimized subsetting with index 'Event'
forder.c received 1 rows and 1 columns
forder took 0 sec
x is already ordered by these columns, no need to call reorder
i.Event has same type (logical) as x.Event. No coercion needed.
on= matches existing index, using index
Starting bmerge ...
bmerge done in 0.000s elapsed (0.000s cpu) 
Constructing irows for '!byjoin || nqbyjoin' ... 0.000s elapsed (0.000s cpu) 
Assigning to 1 row subset of 10 rows
RHS_list_of_columns == true
Dropping index 'Event' due to an update on a key column
Dropping index 'ID' due to an update on a key column

My sessionInfo() is below.


R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.6

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.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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: America/Detroit
tzcode source: internal

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

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

loaded via a namespace (and not attached):
[1] compiler_4.3.1    tools_4.3.1       rstudioapi_0.15.0

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.