Why does “..” work to pass column names in a character vector variable?

前端 未结 1 351
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 21:07

The following code does work but I cannot find any documentation about the \"..\" (dot dot) operator in the data.table help and vignette:



        
相关标签:
1条回答
  • 2021-01-01 21:48

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

    It reads (quoted directly):

    When j is a symbol prefixed with .. it will be looked up in calling scope and its value taken to be column names or numbers.

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

    When you see the .. prefix think one-level-up like the directory .. in all operating systems meaning the parent directory. In future the .. prefix could be made to work on all symbols apearing anywhere inside DT[...]. It is intended to be a convenient way to protect your code from accidentally picking up a column name. Similar to how x. and i. prefixes (analogous to SQL table aliases) can already be used to disambiguate the same column name present in both x and i. A symbol prefix rather than a ..() function will be easier for us to optimize internally and more convenient if you have many variables in calling scope that you wish to use in your expressions safely. This feature was first raised in 2012 and long wished for, #633. It is experimental.

    Note: This answer by Arun led me to this information.

    0 讨论(0)
提交回复
热议问题