I have some questions about substituting names in expressions by strings in a consistent ways across different functions From the dataframe
sample_df <- data.frame(a = 1:5, b = 5:1, c = c(5, 3, 1, 4, 1))
In
lm
, I can use different commands to substitute a regressor by a string in the formulalm(a~get("b"),sample_df) # substituting a part of a formula lm(a~eval(as.name("b")),sample_df) # substituting a part of a formula lm(substitute(a~v,list(v=as.name("b"))),sample_df) # substituting the whole formula lm(eval(substitute(a~v,list(v=as.name("b"))),sample_df)) # substituting the whole formula eval(substitute(lm(a~v,sample_df),list(v=as.name("b")))) # substituting the whole call
What are the differences between all these commands? I can see the first two command gives a regressor named respectively
get("b")
andeval(as.name("b"))
while the others give b. Are there other (maybe more subtle/problematic) differences? Why iseval
irrelevant between 3 and 4?In
data.table
, all works likelm
sample_dt=as.data.table(sample_df) sample_dt[,mean:=mean(get("b"))] sample_dt[,eval(substitute(mean:=mean(v),list(v=as.name("b"))))] eval(substitute( sample_dt[,mean:=mean(v)],list(v=as.name("b"))))
Now, trying to substitute a name by a string in
dplyr
sample_df %>% mutate(mean=mean(get("b"))) eval(substitute(sample_df %>% mutate(mean=mean(v)),list(v=as.name("b"))))
The first looks for an object in the global environment while the second works. How could I predict
get
would not work here while it works inlm
and[.data.table
?