Why does ".." work to skip column names in a character vector variable?

The following code works, but I can’t find the documentation for the ".." operator (period) in the help of data.table and vignette:

 library(data.table) cols <- c("mpg", "gear") DT <- as.data.table(mtcars) DT[, ..cols] 

Output:

  mpg gear 1: 21.0 4 2: 21.0 4 3: 22.8 4 4: 21.4 3 5: 18.7 3 ... 

Why does this work, is there any documentation for this?

PS: Normally, I would use mget , etc ...

Edit 1: This is not a simple R function of reserved names ... , ..1 , ..2 , etc., which are used to refer to arguments passed from the caller (see ?Reserved ). In my example, not a number is used, but characters after two points.

Edit 2: this is not a duplicate , as the Rich Scriven example shows:

 > mtcars[, ..cols] Error in `[.data.frame`(mtcars, , ..cols) : object '..cols' not found 
+8
r data.table
source share
1 answer

This is a new, experimental feature added to data.table v1.10.2. This is explained in the NEW FEATURES section of data.table news for changes in version 1.0.2 .

He reads (quoted directly):

If j is a character with a prefix .. , it will be viewed in the call area, and its value will have column names or numbers.

 myCols = c("colA","colB") DT[, myCols, with=FALSE] DT[, ..myCols] # same 

When you see the prefix .. , think of one level, such as a directory .. on all operating systems, which means the parent directory. In the future, the .. prefix may be made to work with all characters appearing somewhere inside DT[...] . It is designed for a convenient way to protect your code from accidentally selecting a column name. Like prefixes x. and i. (similar to SQL table aliases) can already be used to eliminate the ambiguity of the name of the same column, present in both x and i . The symbol prefix, not the function ..() will be easier for us to optimize internally and more conveniently if you have many variables in the call area that you want to use in your expressions safely. This feature was first raised in 2012 and has long been desired, # 633 . This is experimental.

Note: This answer from Aruna led me to this information.

+7
source share

All Articles