Why does r ~ not recognize this symbol in my model?

mymodel = lm(SalePrice ~ Full bath + Half Bath)
Error: unexpected symbol in "mymodel = lm(SalePrice ~ Full bath"

I don't understand why I can't run this model, there is no problem with my variables.

It is because of your variable names with empty spaces, you have to quote it with backticks.

library(dplyr)
n_cars <- cars
n_cars <- n_cars %>% 
     mutate('n speed' = speed)
lm(dist ~ speed, data = n_cars)
> Call:
> lm(formula = dist ~ speed, data = n_cars)

> Coefficients:
> (Intercept)        speed  
>    -17.579        3.932  

lm(dist ~ n speed, data = n_cars)
> Error: unexpected symbol in "lm(dist ~ n speed"

lm(dist ~ `n speed`, data = n_cars)

> Call:
> lm(formula = dist ~ `n speed`, data = n_cars)

> Coefficients:
> (Intercept)    `n speed`  
>    -17.579        3.932  
3 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.