所以, 我正在学习R. 我在这里关注本教程:https://www.datacamp.com/courses/introduction-to-r/chapter-4-factors?ex=4
这就是我投入的内容:
survey_vector <- c("M","F","F","M","M")
factor_survey_vector <- factor(survey_vector)
# Your code here
levels(factor_survey_vector) <- c("Female","Male")
factor_survey_vector
summary(factor_survey_vector)
这就是它在R
中提示的内容> factor_survey_vector
[1] Male Female Female Male Male
Levels: Female Male
> summary(factor_survey_vector)
Female Male
2 3
虽然我理解factor_survey_vector
的提示,但我不理解summary(factor_survey_vector)
的提示。
R怎么知道,有2个女性和3个男性?我只将矢量c("Female","Male")
指定给levels(factor_survey_vector)
。它如何解释,每个M
是一个男性,每个F
一个女性?我想我在这里监督一些非常微不足道的事情?!
答案 0 :(得分:1)
您可以使用str()
查看基础结构:
> survey_vector <- c("M","F","F","M","M")
> factor_survey_vector <- factor(survey_vector)
>
>
> str(factor_survey_vector)
Factor w/ 2 levels "F","M": 2 1 1 2 2
所以factor_survey_vector
是2 1 1 2 2,等级1是&#34; F&#34;和2级&#34; M&#34;
> levels(factor_survey_vector) <- c("Female","Male")
> str(factor_survey_vector)
Factor w/ 2 levels "Female","Male": 2 1 1 2 2
这里唯一的区别是关卡标签已经改变。现在1是女性,2是男性。