dt[, (cols) := list(...), by = group] should not silently recycles list
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the grouped := examples in the issue and comparing them with the ungrouped behavior. Trace the data.table grouped assignment path to find where list lengths are recycled or truncated, then add regression coverage for fewer and more values than target columns. Done means mismatched lengths no longer silently produce results and the proposed zero-, one-, or .N-element behavior is verified.
Written by the indexing model from the issue text.
Description
Currently, dt[, (cols) := list(...), by = group] seems to silently recycles list(...) when replacing values of cols. If length(list) < length(cols), then list is recyled; if length(list) > length(cols) then redundant elements in list are silently dropped, as demonstrated below:
When by = group is absent, the lengths are checked:
library(data.table)
dt <- data.table(id = 1:10)
xn <- 1:3
xcols <- paste0("x", xn)
dt[, (xcols) := list(10, 20)]
#> Error in `[.data.table`(dt, , `:=`((xcols), list(10, 20))): Supplied 3 columns to be assigned 2 items. Please see NEWS for v1.12.2.
However, if by = group is used, list is recycled:
library(data.table)
dt <- data.table(id = 1:10)
dt[, group := sample(1:2, .N, replace = TRUE)]
xn <- 1:3
xcols <- paste0("x", xn)
dt[, (xcols) := list(10, 20), by = group]
dt
#> id group x1 x2 x3
#> 1: 1 2 10 20 10
#> 2: 2 2 10 20 10
#> 3: 3 2 10 20 10
#> 4: 4 2 10 20 10
#> 5: 5 2 10 20 10
#> 6: 6 2 10 20 10
#> 7: 7 1 10 20 10
#> 8: 8 2 10 20 10
#> 9: 9 2 10 20 10
#> 10: 10 2 10 20 10
library(data.table)
dt <- data.table(id = 1:10)
dt[, group := sample(1:2, .N, replace = TRUE)]
xn <- 1:3
xcols <- paste0("x", xn)
dt[, (xcols) := list(40, 30, 20, 10), by = group]
dt
#> id group x1 x2 x3
#> 1: 1 1 40 30 20
#> 2: 2 1 40 30 20
#> 3: 3 2 40 30 20
#> 4: 4 2 40 30 20
#> 5: 5 2 40 30 20
#> 6: 6 1 40 30 20
#> 7: 7 1 40 30 20
#> 8: 8 2 40 30 20
#> 9: 9 2 40 30 20
#> 10: 10 1 40 30 20
Personally, the recycling behavior is almost always unwanted. If it occurs, it is mostly something wrong with my code.
Consider the following example where list(...) is produced by lapply(.SD, ...). If the function is inlined and a bit complicated, one often forgets to write .SDcols.
library(data.table)
set.seed(123)
dt <- data.table(id = 1:10)
dt[, group := sample(1:2, .N, replace = TRUE)]
xn <- 1:3
xcols <- paste0("x", xn)
for (i in xn) {
dt[, xcols[[i]] := runif(.N)]
}
dt[, (xcols) := lapply(.SD, function(x) {
x / sd(x)
}), by = group]
dt
#> id group x1 x2 x3
#> 1: 1 1 0.2672612 2.7645427 3.2041655
#> 2: 2 1 0.5345225 1.3098014 2.4955128
#> 3: 3 1 0.8017837 1.9576795 2.3071378
#> 4: 4 2 2.3421602 1.5214175 4.9351189
#> 5: 5 1 1.3363062 0.2973764 2.3618854
#> 6: 6 2 3.5132403 2.3907258 3.5168344
#> 7: 7 2 4.0987803 0.6538253 2.7005050
#> 8: 8 2 4.6843204 0.1117471 2.9490603
#> 9: 9 1 2.4053512 0.9474491 1.0415680
#> 10: 10 1 2.6726124 2.7578116 0.5299108
Undesired/incorrect results are silently produced. The following are the correct results with .SDcols added.
library(data.table)
set.seed(123)
dt <- data.table(id = 1:10)
dt[, group := sample(1:2, .N, replace = TRUE)]
xn <- 1:3
xcols <- paste0("x", xn)
for (i in xn) {
dt[, xcols[[i]] := runif(.N)]
}
dt[, (xcols) := lapply(.SD, function(x) {
x / sd(x)
}), by = group, .SDcols = xcols]
dt
#> id group x1 x2 x3
#> 1: 1 1 2.7645427 3.2041655 2.5018371
#> 2: 2 1 1.3098014 2.4955128 2.3440794
#> 3: 3 1 1.9576795 2.3071378 1.7943807
#> 4: 4 2 1.5214175 4.9351189 2.9399476
#> 5: 5 1 0.2973764 2.3618854 0.0639438
#> 6: 6 2 2.3907258 3.5168344 1.7658739
#> 7: 7 2 0.6538253 2.7005050 2.8031711
#> 8: 8 2 0.1117471 2.9490603 0.7998165
#> 9: 9 1 0.9474491 1.0415680 0.8266013
#> 10: 10 1 2.7578116 0.5299108 0.6017398
I suggest that list(...) recycling should be consistent with the behavior data.table has already adopted with row recycling: only accepting zero, one, or .N elements.
- 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 ·