简体   繁体   中英

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

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

> 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) . How does R know, that there are 2 Females and 3 Males? I only assigned the vector c("Female","Male") to levels(factor_survey_vector) . How can it interpret, that each M is a Male and each F a Female? I guess I am overseeing something very trivial here?!

You can use str() to look at the underlying structure:

> 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"

> 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.

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