-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
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:
Line 400 in 3d54417
| if (is.logical(i)) { |
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.