简体   繁体   中英

How to convert categorical variables into quantitative variables in R

I have a data set with 100 inputs, they are either called X or Y. I am trying to use the anova function to compare these categorical X's and Y's to a quantitative variable (length). How do I convert my categorical variable to something quantitative? Thanks

You may not need to to do any conversion: the factor and character data types are accepted by lm and anova(). It;s possible that you are thinking of aov which is for balanced designs. lm is the regression function that will handle the unbalanced linear models.

> set.seed(123)
> typ <- sample(c("X", "Y"), 100, prob=c(1,2)/3, replace=TRUE)
> num <- rnorm(100) + (typ=="Y")
> dfrm <- data.frame(num =num, typ =typ)

> fit<-lm(num~typ, data=dfrm)
> anova(fit)
Analysis of Variance Table

Response: num
          Df Sum Sq Mean Sq F value    Pr(>F)    
typ        1 21.422 21.4225  22.787 6.331e-06 ***
Residuals 98 92.133  0.9401                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
> fit

Call:
lm(formula = num ~ typ, data = dfrm)

Coefficients:
(Intercept)         typY  
   -0.04325      0.98433  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM