Reputation: 959
I have names of columns of data.table
stored in variables. I need to refer to the columns via these variables. I made the code working (example below), but I have no clue why I need to use sometimes get()
and sometime eval()
. Can somebody clarify this, please?
# 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
Upvotes: 4
Views: 751
Reputation: 5532
This example shows the difference between how eval
and get
differ in function. Using a data.table
object is not needed to show what each does.
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
Since this question is more about eval
vs. get
, I will leave the data.table
specifics out. The way data.table
handles strings and variable names is very likely answered in a different SO post.
Upvotes: 5