r - Why does ".." work to pass column names in a character vector variable? -


the following code work cannot find documentation ".." (dot dot) operator in data.table , vignette:

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

the output is:

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

why work, there documentation that?

ps: use mget etc...

edit 1: not plain r feature of reserved names ..., ..1, ..2 etc., used refer arguments passed down calling function (see ?reserved). example uses not number, characters after 2 dots.

edit 2: no duplicate, example of rich scriven shows:

> mtcars[, ..cols] error in `[.data.frame`(mtcars, , ..cols) : object '..cols' not found 

this new, experimental feature added in data.table v1.10.2. explained in new features section of data.table news changes in v1.10.2.

it reads (quoted directly):

when j symbol prefixed .. looked in calling scope , value taken column names or numbers.

mycols = c("cola","colb") dt[, mycols, with=false] dt[, ..mycols]              # same 

when see .. prefix think one-level-up directory .. in operating systems meaning parent directory. in future .. prefix made work on symbols apearing anywhere inside dt[...]. intended convenient way protect code accidentally picking column name. similar how x. , i. prefixes (analogous sql table aliases) can used disambiguate same column name present in both x , i. symbol prefix rather ..() function easier optimize internally , more convenient if have many variables in calling scope wish use in expressions safely. feature first raised in 2012 , long wished for, #633. experimental.

note: this answer arun led me information.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -