1

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?

nicholas
  • 903
  • 2
  • 12

2 Answers2

1

by do.call and as.formula,

rhs <- paste("mpg ~", rhs)
rhs

"mpg ~ disp + hp"

do.call("lm", list(as.formula(rhs), data = as.name("mtcars")))

Call:
lm(formula = mpg ~ disp + hp, data = mtcars)

Coefficients:
(Intercept)         disp           hp  
   30.73590     -0.03035     -0.02484 
Park
  • 14,771
  • 6
  • 10
  • 29
1

your rhs object is a string. If you have formulas stored as strings, you have to first convert to formula with as.formula, then use it as you would normally.

rhs <- paste(c("disp", "hp"), collapse = " + ")
tilde <- ' ~ '
lhs <- 'mpg ~ '

lm(data = mtcars, formula=as.formula(paste0(lhs, tilde, rhs)))

Call:
lm(formula = as.formula(paste0(lhs, tilde, rhs)), data = mtcars)

Coefficients:
(Intercept)         disp           hp  
   30.73590     -0.03035     -0.02484  

As suggested in the comment by @rawr, you can also use reformulate:

lm(data = mtcars, formula=reformulate(termlabels = rhs, response = lhs))
GuedesBF
  • 8,409
  • 5
  • 19
  • 37