0

Let's say you have a categorical variable but the categories are numbers. Is there a way to within the lm() formula, let R know that a specific regressor is categorical not numerical?

Thanks!

user6472523
  • 211
  • 3
  • 8
  • isn't this unnecessary? my memory is you don't need to specify – 3pitt May 10 '18 at 15:06
  • 2
    How about `as.factor()`? – Gopala May 10 '18 at 15:13
  • 3
    Yes, make them factors within your data frame, or wrap them each in `as.factor` inside your formula. But if you're trying to shortcut it like `lm(y ~ ., data = df)`, make those columns factors in your df first – camille May 10 '18 at 15:16
  • 3
    @MikePalmice Their categories are numbers so it will treat them as continuous if you don't specify to treat them as categories by converting to a factor. – Dason May 10 '18 at 15:16
  • 2
    If the categorical variables are binary then you get the same model.matrix whether they are factor or numeric. – G. Grothendieck May 10 '18 at 15:25
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 10 '18 at 16:15

0 Answers0