How to change function argument name. For example using substitute I can change function argument value or function name:
substitute(quote(F(x= A)), list(A= quote(B), F= quote(G)))
## result
quote(G(x = B))
but this doesn’t work:
substitute(quote(F(x= A)), list(x= quote(y)))
## result
quote(F(x = A))
#
EDIT (@Joran here is real example, maybe not so real but very close to what I am doing)
#
library("multcomp")
data("mtcars")
mtcars$gear <- factor(mtcars$gear)
mtcars$cyl <- factor(mtcars$cyl)
xv <- c("gear","cyl")
for(v in xv){
fo <- as.formula(paste("mpg",v,sep="~"))
fit <- lm(fo,data=mtcars)
print(eval(substitute(summary(glht(fit,linfct= mcp(vn="Dunnett"))),list(vn=v))))
}
Taking your example of something similar to the actual problem, why not do this:
Which gives:
The trick here is to note that the first argument to
mcpis...which usually means we can pass in a list of the formlist(tag = value). We can’t specifytagasvhere, so just create the listllwith a single element"Dunnett"and then change the names attribute of this list in the loop to bev. Then usedo.call()to arrange to callmcp()with this argument list.And for completeness, as @Josh metions in a comment above, from this answer from @Hadley the list can be more succinctly stated using the
setNames()function: