Looking at example(data.table), there's:
library(data.table)
DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
DT[!2:4] # all rows other than 2:4
DT[-(2:4)] # same
But I didn't remember that this was a feature, and I guess the inconsistency with base R is unnecessary friction for people coming from base R and/or trying to understand the behavior by looking at help("!"), DT[, !2:4], etc.^^
This came up on SO from @moodymudskipper using DT2[!lengths(some_list_col)] to select rows where the list element had nonzero length.
Edit: There is already an exception to the rule that ! triggers a not-join when i=!logi:
I guess a similar check could appear somewhere after that
if (is.integer(i)) {
if (notjoin) {
notjoin = FALSE
i = !i
}
}
Or more generally the condition if (!is.list(i)) would match my understanding/expectations.
As IceCreamToucan notes on SO, packages depending on this behavior could be an obstacle to making the change that I had not considered.
^^ Re "why DT[, !2:4]?", my usual suggestion for understanding what's going on with x[z] is to inspect x[, z] but that doesn't work in this case due to the convenience features for column selection. Maybe I should suggest x[, print(z)] instead.
Looking at
example(data.table), there's:But I didn't remember that this was a feature, and I guess the inconsistency with base R is unnecessary friction for people coming from base R and/or trying to understand the behavior by looking at
help("!"),DT[, !2:4], etc.^^This came up on SO from @moodymudskipper using
DT2[!lengths(some_list_col)]to select rows where the list element had nonzero length.Edit: There is already an exception to the rule that ! triggers a not-join when
i=!logi:data.table/R/data.table.R
Line 400 in 3d54417
I guess a similar check could appear somewhere after that
Or more generally the condition
if (!is.list(i))would match my understanding/expectations.As IceCreamToucan notes on SO, packages depending on this behavior could be an obstacle to making the change that I had not considered.
^^ Re "why
DT[, !2:4]?", my usual suggestion for understanding what's going on withx[z]is to inspectx[, z]but that doesn't work in this case due to the convenience features for column selection. Maybe I should suggestx[, print(z)]instead.