Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

r - Select a sequence of columns: `:` works but not `seq`

I'm trying to subset a dataset by selecting some columns from a data.table. However, my code does not work with some variations.

Here is a sample data.table

library(data.table)
DT <- data.table( ID = 1:50,
            Capacity = sample(100:1000, size = 50, replace = F),
            Code = sample(LETTERS[1:4], 50, replace = T),
            State = rep(c("Alabama","Indiana","Texas","Nevada"), 50))

Here is a working subset code, where a numeric sequence of columns is specified using ::

DT[ , 1:2]

However, specifying the same sequence of columns using seq does not work:

DT[ , seq(1:2)]

Note that this works with a dataframe but not with a data.table.

I need something along the lines of the second format because I'm subsetting based on the output of grep() and it gives the same output as the second format. What am I doing incorrectly?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On recent versions of data.table, numbers can be used in j to specify columns. This behaviour includes formats such as DT[,1:2] to specify a numeric range of columns. (Note that this syntax does not work on older versions of data.table).

So why does DT[ , 1:2] work, but DT[ , seq(1:2)] does not? The answer is buried 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 can see here that data.table is automatically setting the with = FALSE parameter for you when it detects the use of function : in j. It doesn't have the same functionality built in for seq, so we have to specify with = FALSE ourselves if we want to use the seq syntax.

DT[ , seq(1:2), with = FALSE]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...