我将 data.table
的列名存储在变量中。我需要通过这些变量来引用列。我使代码工作(下面的示例),但我不知道为什么我有时需要使用 get()
有时需要使用 eval()
。有人可以澄清一下吗?
# generate some data
foo <- rep(1:2,each = 3)
bar <- rep(c("A","B","C"),2)
baz <- rep(1:5,2)[1:6]
df <- data.frame(foo,bar,baz)
setDT(df)
# refer to columns directly by their names
df[, "qux":=baz-baz[bar=="C"], by=foo]
# save column names into variables and call columns via these variables
var1 <- "foo"
var2 <- "bar"
var3 <- "baz"
varNew <- "qux2"
df[, eval(varNew) := get(var3) - get(var3)[get(var2) == "C"], by = get(var1)]
df
foo bar baz qux qux2
1: 1 A 1 -2 -2
2: 1 B 2 -1 -1
3: 1 C 3 0 0
4: 2 A 4 3 3
5: 2 B 5 4 4
6: 2 C 1 0 0
最佳答案
此示例显示了 eval
和 get
在功能上的区别。不需要使用 data.table
对象来显示每个对象的作用。
iVec <- c(123, 456)
iVarName <- "iVec"
# Returns the contents of 'iVarName' (a string). This happens
# to be the name of a variable but doesn't have to.
eval(iVarName)
##> [1] "iVec"
# Returns the contents of what 'iVarName' refers to (it
# refers to the variable "iVec" in this case, which
# is a variable which contains a vector of integers).
get(iVarName)
##> [1] 123 456
### #########################################
### Similar to above but where the variable
### 'iVec2' does not exist.
### #########################################
rm(iVec2)
# The variable "iVec2" does not exist.
iVarName2 <- 'iVec2'
# Returns the contents of 'iVarName2' (a string). This is not
# the name of an existing variable in this context.
eval(iVarName2)
## [1] "iVec2"
get(iVarName2) # Returns an error because 'iVec2' doesn't exist.
## Error in get(iVarName2) : object 'iVec2' not found
因为这个问题更多的是关于eval
和 get
,所以我将省略 data.table
细节。 data.table
处理字符串和变量名称的方式很可能在不同的 SO 帖子中得到回答。
关于R - 将列名传递到 data.table 公式中 - get 和 eval 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667359/