How can I call a concatenated vector of variable names on the right-hand side of a formula?
For example, this works:
lm(data = mtcars, mpg ~ disp + hp)
But this does not:
rhs <- paste(c("disp", "hp"), collapse = " + ")
lm(data = mtcars, mpg ~ rhs)
#> Error in model.frame.default(formula = mpg ~ rhs, data = mtcars, drop.unused.levels = TRUE): variable lengths differ (found for 'rhs')
How can I avoid this error?