rdataframeerror-handlingdata.table

data.table use df[, i] to fetch a column


I am using data.table package.

library(data.table)
df = data.table(col1 = c(2,4,6), 
                col2 = c(4,6,8), 
                col3 = c(8,10,12), 
                col4 = c(20,16,14))
i = 2
df[,3]
df[,i+1]
df[,i]
df[,..i]
df[,..(i+1)]

df[,3] gives the third column:

   col3
1:    8
2:   10
3:   12

But df[,i+1] only gives a number 3. This is what I don't understand. It works fine for native data.frame. Why can't it work like a data.frame do?

Furthermore, df[,i] raised error, which suggests me use df[,..i] and it gives the second column. Yet, df[,..(i+1)] raised error: Error in ..(i + 1) : could not find function "..".

Somebody mind help me understand the differences?

Tell the differences among these tryings. What is the mechanism here?


Solution

  • You can read the help document of data.table and see how to use with = when you type ?..

    
    > df[, i + 1, with = FALSE]
        col3
       <num>
    1:     8
    2:    10
    3:    12
    
    > df[, i + 1]
    [1] 3
    

    By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables. In case of overlapping variables names inside dataset and in parent scope you can use double dot prefix ..cols to explicitly refer to 'cols variable parent scope and not from your dataset.

    When j is a character vector of column names, a numeric vector of column positions to select or of the form startcol:endcol, and the value returned is always a data.table. with=FALSE is not necessary anymore to select columns dynamically. Note that x[, cols] is equivalent to x[, ..cols] and to x[, cols, with=FALSE] and to x[, .SD, .SDcols=cols].