In recent versions of data.table, numbers can be used in j to indicate columns. This behavior includes formats such as DT[,1:2] to indicate the numerical range of columns. (Note that this syntax does not work in older versions of data.table).
So why does DT[,1:2] , but DT[,seq(1:2)] does not? The answer is incorrect in the code for data.table:::[.data.table , which includes the lines:
if (!missing(j)) { jsub = replace_dot_alias(substitute(j)) root = if (is.call(jsub)) as.character(jsub[[1L]])[1L] else "" if (root == ":" || (root %chin% c("-", "!") && is.call(jsub[[2L]]) && jsub[[2L]][[1L]] == "(" && is.call(jsub[[2L]][[2L]]) && jsub[[2L]][[2L]][[1L]] == ":") || (!length(all.vars(jsub)) && root %chin% c("", "c", "paste", "paste0", "-", "!") && missing(by))) { with = FALSE }
We see here that data.table automatically sets the with = FALSE parameter for you when it detects the use of a function : in j . It does not have the same functionality as for seq , so we need to specify with = FALSE if we want to use the seq syntax.
DT[,seq(1:2), with=FALSE]
source share