Assigning some values to randomly selected rows leads to incorrect results
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
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
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
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 ·