为什么“ ..”在字符向量变量中传递列名的方法? [英] Why does ".." work to pass column names in a character vector variable?

查看:45
本文介绍了为什么“ ..”在字符向量变量中传递列名的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可以正常工作,但是在 data.table <中找不到关于 .. (点点)的任何文档。 / code>帮助和插图:

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

输出为:

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

为什么这样做,是否有任何相关文档?



PS:通常我会使用 mget 等...



编辑1:保留名称 ... .. 1 .. 2的普通R功能等,用于引用从调用函数传递的参数(请参见?保留)。我的示例使用的不是数字,而是两个点后的字符。



编辑2:这不是重复的,例如Rich Scriven显示:

 > mtcars [,..cols] 
`[.data.frame`(mtcars,,..cols):错误'..cols'找不到


解决方案

这是 data.table

中新增的实验性功能 em> v1.10.2。在 新功能部分



它显示为(


j 是以<$ c $为前缀的符号时c> .. 将在调用范围中进行查找,并将其值视为列名或数字。

  myCols = c( colA, colB)
DT [,myCols,with = FALSE]
DT [,..myCols]#相同的

当您看到时。前缀认为一级,如所有操作系统中的目录 .. ,即父目录。将来, .. 前缀可以用于在 DT [...] 内部出现的所有符号。它旨在作为一种方便的方法来保护您的代码以免意外选择列名。类似于 x。 i。前缀(类似于SQL表别名)的用法,已经可以消除它们的歧义了 x i 中都存在列名称。符号前缀而不是 ..() 函数将使我们更易于内部优化,如果调用范围中有很多变量,则更方便您希望在表达式中安全使用的代码。此功能于2012年首次提出,并一直希望#633


注意: 阿伦(Arun)的回答使我明白了这一信息。


The following code does work but I cannot find any documentation about the ".." (dot dot) operator in the data.table help and 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 does this work, is there any documentation for that?

PS: Normally I would use mget etc...

Edit 1: This is not a plain R feature of the reserved names ..., ..1, ..2 etc., which are used to refer to arguments passed down from a calling function (see ?Reserved). My example uses not a number, but characters after the two dots.

Edit 2: This is no duplicate, as the example of Rich Scriven shows:

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

解决方案

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.

这篇关于为什么“ ..”在字符向量变量中传递列名的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆