How factor one variable in a large model in r? I apologize in advance if the question is duplicate.
I have 100 variables as predictors, in which 1 variable is categorical, like 0 or 1. I want to factor this variable in the model. The problem is, if I use factor(X1) + .,, X1 and factor (X1) would both appear in the summary of the model. I tried to write out factor(X1) + other variables, but I cannot, because the number of remain variables are too many.
 A: You are right:
lm(mpg~as.factor(cyl)+., data=mtcars)

Call:
lm(formula = mpg ~ as.factor(cyl) + ., data = mtcars)

Coefficients:
    (Intercept)  as.factor(cyl)6  as.factor(cyl)8              cyl             disp               hp             drat  
       17.81984         -1.66031          1.63744               NA          0.01391         -0.04613          0.02635  
             wt             qsec               vs               am             gear             carb  
       -3.80625          0.64696          1.74739          2.61727          0.76403          0.50935  

What you could do is to change the variable of interest to factor before the analysis:
> mtcars$cyl <- as.factor(mtcars$cyl)
> lm(mpg~cyl+., data=mtcars)

Call:
lm(formula = mpg ~ cyl + ., data = mtcars)

Coefficients:
(Intercept)         cyl6         cyl8         disp           hp         drat           wt         qsec           vs  
   17.81984     -1.66031      1.63744      0.01391     -0.04613      0.02635     -3.80625      0.64696      1.74739  
         am         gear         carb  
    2.61727      0.76403      0.50935

as you can see, now it works. Thanks for mentioning this one, I didn't notice that it works like this before (+1).
Generally, . in formula tells R to include "all the other" variables in the model. In this case it probably didn't mach as.factor(cyl) to cyl and considered it as a different variable.
