[英]Create factor levels in R and summary() function
So, I am learning R. I am following this tutorial here: https://www.datacamp.com/courses/introduction-to-r/chapter-4-factors?ex=4 因此,我正在学习R。我在这里关注此教程: https : //www.datacamp.com/courses/introduction-to-r/chapter-4-factors?ex=4
Thats what I put in: 那就是我输入的内容:
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)
And thats what it prompts out in R 这就是R中提示的内容
> factor_survey_vector
[1] Male Female Female Male Male
Levels: Female Male
> summary(factor_survey_vector)
Female Male
2 3
While I understand the prompt of factor_survey_vector
I do not understand the prompt of the summary(factor_survey_vector)
. 虽然我了解factor_survey_vector
的提示, factor_survey_vector
我不了解summary(factor_survey_vector)
的提示。 How does R know, that there are 2 Females and 3 Males? R怎么知道有2个雌性和3个雄性? I only assigned the vector c("Female","Male")
to levels(factor_survey_vector)
. 我只将向量c("Female","Male")
分配给levels(factor_survey_vector)
。 How can it interpret, that each M
is a Male and each F
a Female? 如何解释每个M
是男性,每个F
是女性? I guess I am overseeing something very trivial here?! 我想我正在监督一些非常琐碎的事情?!
You can use str()
to look at the underlying structure: 您可以使用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
So factor_survey_vector
is a 2 1 1 2 2 with level 1 being "F" and level 2 "M" 因此factor_survey_vector
是2 1 1 2 2 2,级别1为“ F”,级别2为“ M”
> levels(factor_survey_vector) <- c("Female","Male")
> str(factor_survey_vector)
Factor w/ 2 levels "Female","Male": 2 1 1 2 2
Here the only difference is the level labels have changed. 唯一的区别是级别标签已更改。 Now 1 is Female, 2 is Male. 现在1是女性,2是男性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.