Search code examples
rmetaprogrammingquoting

How to quote/unquote variables for constructing formulas in R


I am having a hard time understanding how quoting, unquoting, quasiquotation... works in R. In this case I wanted to fit a linear model. I think ususally you do not need to quote the input to the lm-call.

So I wanted to do something like this:

names(mtcars)

y = "mpg" # response
x = "hp" # predictor

fml = y ~ x

lm(fml, data=mtcars)

But obviously this does not work as literally "y" and "x" are passed int the formula. I tried several things with expr() and eval() but I think I am not on the right way. Reading the chapter on Metaprogramming in advanced R would certainly help a lot, but maybe there is also an "intuitive" solution here.


Solution

  • For lm you don't need quoting/unquoting. You can use as.formula or reformulate to construct the formula.

    lm(reformulate(y, x), data=mtcars)
    

    However, since lm also accepts strings that can be coerced to formula you can use paste which will give the same result.

    lm(paste(x, y, sep = '~'), data=mtcars)