Skip to content

Commit 0fa568e

Browse files
.SDcols is a negation only for _unary_ '-' (#5881)
* fix issue with .SDcols using binary "-"
1 parent dd53245 commit 0fa568e

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
342342
23. `fread(fill=TRUE, verbose=TRUE)` would segfault on the out-of-sample type bump verbose output if the input did not contain column names, [5046](https://github.com/Rdatatable/data.table/pull/5046). Thanks to Václav Tlapák for the PR.
343343
344-
24. `.SDcols=-V2:-V1` and `.SDcols=(-1)` could error with `xcolAns does not pass checks` and `argument specifying columns specify non existing column(s)`, [#4231](https://github.com/Rdatatable/data.table/issues/4231). Thanks to Jan Gorecki for reporting and the PR.
344+
24. `.SDcols=-V2:-V1` and `.SDcols=(-1)` could error with `xcolAns does not pass checks` and `argument specifying columns specify non existing column(s)`, [#4231](https://github.com/Rdatatable/data.table/issues/4231). Thanks to Jan Gorecki for reporting and the PR. Thanks Toby Dylan Hocking for tracking down an error caused by the initial fix and Michael Chirico for fixing it.
345345
346346
25. `.SDcols=<logical vector>` is now documented in `?data.table` and it is now an error if the logical vector's length is not equal to the number of columns (consistent with `data.table`'s no-recycling policy; see new feature 1 in v1.12.2 Apr 2019), [#4115](https://github.com/Rdatatable/data.table/issues/4115). Thanks to @Henrik-P for reporting and Jan Gorecki for the PR.
347347

R/data.table.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,8 @@ replace_dot_alias = function(e) {
10081008
# peel from parentheses before negation so (-1L) works as well: as.data.table(as.list(1:3))[, .SD,.SDcols=(-1L)] #4231
10091009
while(colsub %iscall% "(") colsub = as.list(colsub)[[-1L]]
10101010
# fix for R-Forge #5190. colsub[[1L]] gave error when it's a symbol.
1011-
if (colsub %iscall% c("!", "-")) {
1011+
# NB: _unary_ '-', not _binary_ '-' (#5826). Test for '!' length-2 should be redundant but low-cost & keeps code concise.
1012+
if (colsub %iscall% c("!", "-") && length(colsub) == 2L) {
10121013
negate_sdcols = TRUE
10131014
colsub = colsub[[2L]]
10141015
} else negate_sdcols = FALSE

inst/tests/tests.Rraw

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18179,3 +18179,8 @@ DT = data.table(id=1:2, x=1:2)
1817918179
r = copy(DT)[1L, x:= 5L]
1818018180
test(2241.13, DT, data.table(id=1:2, x=1:2))
1818118181
test(2241.14, r, data.table(id=1:2, x=c(5L,2L)))
18182+
18183+
# .SDcols using binary '-' is evaluated correctly (#5826 exposed this)
18184+
DT = data.table(a=1, b=2, c=3)
18185+
test(2242.1, DT[, .SD, .SDcols=2-1], DT[, .(a)])
18186+
test(2242.2, DT[, .SD, .SDcols=length(DT)-1], DT[, .SD, .SDcols=2])

0 commit comments

Comments
 (0)