![](/img/trans.png)
[英]How can I represent in R two quantitative variables and color the plot according to a categorical variable?
[英]How to convert categorical variables into quantitative variables in R
我有一個包含100個輸入的數據集,它們被稱為X或Y。我試圖使用anova函數將這些類別X和Y與定量變量(長度)進行比較。 如何將分類變量轉換為定量變量? 謝謝
您可能不需要進行任何轉換:lm和anova()接受factor和character數據類型。 您可能正在考慮針對平衡設計的aov。 lm是將處理不平衡線性模型的回歸函數。
> 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.