当x也是数据表中的列时,get(x)在R data.table中不起作用 [英] get(x) does not work in R data.table when x is also a column in the data table
问题描述
我注意到当x还是同一数据表中的列时,get(x)在R数据表中不起作用。请参见下面的代码段。在编写将数据表作为输入的R函数时,很难完全避免。这是R data.table包中的错误吗?谢谢!
I noticed that get(x) does not work in R data table when x is also a column in the same data table. See the code snippet below. This is hard to avoid completely when writing an R function which takes the data table as an input. Is this a bug in the R data.table package? Thanks!
library(data.table)
dt = data.table(x=1:3, y=2:4)
var = 'y'
x = 'y'
dt[, 3*get(var)] # [1] 6 9 12
dt[, 3*get(x)] # Error in get(x): invalid first argument
推荐答案
通常,当列和变量之间存在命名冲突时,列将优先。从data.table v1.10.2(2017年1月31日)开始,澄清名称为 not 列名称的首选方法是使用 ..
前缀[ 1 ]:
In general, when there is a naming conflict between columns and variables, columns will take precedence. Since v1.10.2 (31 Jan 2017) of data.table, the preferred approach to clarify that a name is a not a column name is to use the ..
prefix [1]:
当j是以
为前缀的符号时。 。
将在调用范围中进行查找,并将其值用作列名或数字。
当您看到..
前缀时,请考虑上一层,例如目录中的..
所有操作系统均指父目录。
将来,可以使..
前缀适用于所有在DT [...] $ c内部出现的符号$ c>。 ...
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. When you see the..
prefix think one-level-up, like the directory..
in all operating systems means the parent directory. In future the..
prefix could be made to work on all symbols apearing anywhere insideDT[...]
. ...
我们相信 ..
的主要关注点是解决 var
在调用范围中, var
也是列名。此外,我们还没有忘记过去建议在调用范围中给变量加上 ..
前缀。如果您这样做了,并且 .. var
在调用范围中存在,只要 var
在调用范围中都不存在,它仍然可以工作也没有 .. var
作为列名存在。现在,请在调用范围中删除 .. var
上的 ..
前缀以进行清理。将来,data.table将开始对此类用法发出警告/错误。
Our main focus here which we believe ..
achieves is to resolve the more common ambiguity when var
is in calling scope and var
is a column name too. Further, we have not forgotten that in the past we recommended prefixing the variable in calling scope with ..
yourself. If you did that and ..var
exists in calling scope, that still works, provided neither var
exists in calling scope nor ..var
exists as a column name. Please now remove the ..
prefix on ..var
in calling scope to tidy this up. In future data.table will start to warn/error on such usage.
在您的情况下,您可以 get(.. x)
强制名称 x
在调用范围而不是在data.table环境中进行解析:
In your case, you can get(..x)
to force the name x
to be resolved in calling scope rather than within the data.table environment:
library(data.table)
dt = data.table(x=1:3, y=2:4)
var = 'y'
x = 'y'
dt[, 3*get(var)] # [1] 6 9 12
dt[, 3*get(x)] # Error in get(x): invalid first argument
dt[, 3*get(..x)] # [1] 6 9 12
..
前缀仍处于实验阶段,因此受到限制文档,但在 data.table
的帮助页面上已简要提及:
The ..
prefix is still somewhat experimental and thus has limited documentation, but it is mentioned briefly on the help page for data.table
:
默认情况下,
with = TRUE
和j
在x $的范围内求值c $ c>;列名称可用作变量。如果数据集内部和父范围中的变量名称重叠,则可以使用双点前缀
.. cols
显式引用'cols $ c $ p>
By default
with=TRUE
andj
is evaluated within the frame ofx
; 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.
这不是一个bug,更不幸的是 with = T
允许在数据环境中将列用作变量。确实,您可以使用<$ c $ pos> $ pos
或 envir
参数以更基本的R方式避免此问题。 $ c> get()。
This is less a bug and more an unfortunate but natural consequence of with = T
to allow using columns as variables in a data environment. Indeed, you could avoid this issue in a more base R way by using the pos
or envir
argument of get()
.
这篇关于当x也是数据表中的列时,get(x)在R data.table中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!