我在变量中存储了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
答案 0 :(得分:4)
此示例显示了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帖子中得到解答。