Reputation: 12394
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?!
Upvotes: 0
Views: 201
Reputation: 9687
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.
Upvotes: 1