If you want to take our free Intro to R course, here is the link.
Factor Levels
When you first get a data set, you will often notice that it contains factors with specific factor levels. However, sometimes you will want to change the names of these levels for clarity or other reasons. R allows you to do this with the function levels():
levels(factor_vector) <- c("name1", "name2",...)
A good illustration is the raw data that is provided to you by a survey. A common question for every questionnaire is the sex of the respondent. Here, for simplicity, just two categories were recorded, "M" and "F". (You usually need more categories for survey data; either way, you use a factor to store the categorical data.)
survey_vector <- c("M", "F", "F", "M", "M")
Recording the sex with the abbreviations "M" and "F" can be convenient if you are collecting data with pen and paper, but it can introduce confusion when analyzing the data. At that point, you will often want to change the factor levels to "Male" and "Female" instead of "M" and "F" for clarity.
Watch out: the order with which you assign the levels is important. If you type levels(factor_survey_vector), you'll see that it outputs [1] "F" "M". If you don't specify the levels of the factor when creating the vector, R will automatically assign them alphabetically. To correctly map "F" to "Female" and "M" to "Male", the levels should be set to c("Female", "Male"), in this order.
Instructions
- Check out the code that builds a factor vector from
survey_vector. You should use factor_survey_vector in the next instruction.
- Change the factor levels of
factor_survey_vector to c("Female", "Male"). Mind the order of the vector elements here.
# Code to build factor_survey_vector
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
# Specify the levels of factor_survey_vector
levels(factor_survey_vector) <-
factor_survey_vector
Summarizing a Factor
After finishing this course, one of your favorite functions in R will be summary(). This will give you a quick overview of the contents of a variable:
Going back to our survey, you would like to know how many "Male" responses you have in your study, and how many "Female" responses. The summary() function gives you the answer to this question.
Instructions
Ask a summary() of the survey_vector and factor_survey_vector. Interpret the results of both vectors. Are they both equally useful in this case?
# Build factor_survey_vector with clean levels
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")
factor_survey_vector
# Generate summary for survey_vector
# Generate summary for factor_survey_vector
Ordered Factors
Since "Male" and "Female" are unordered (or nominal) factor levels, R returns a warning message, telling you that the greater than operator is not meaningful. As seen before, R attaches an equal value to the levels for such factors.
But this is not always the case! Sometimes you will also deal with factors that do have a natural ordering between its categories. If this is the case, we have to make sure that we pass this information to R...
Let us say that you are leading a research team of five data analysts and that you want to evaluate their performance. To do this, you track their speed, evaluate each analyst as "slow", "medium" or "fast", and save the results in speed_vector.
Instructions
As a first step, assign speed_vector a vector with 5 entries, one for each analyst. Each entry should be either "slow", "medium", or "fast". Use the list below:
- Analyst 1 is medium,
- Analyst 2 is slow,
- Analyst 3 is slow,
- Analyst 4 is medium and
- Analyst 5 is fast.
No need to specify these are factors yet.
# Create speed_vector
speed_vector <-
If you want to learn more from this course, here is the link.
Check out out Factors in R Tutorial.
If you want to take our free Intro to R course, here is the link.
Factor Levels
When you first get a data set, you will often notice that it contains factors with specific factor levels. However, sometimes you will want to change the names of these levels for clarity or other reasons. R allows you to do this with the function
levels():A good illustration is the raw data that is provided to you by a survey. A common question for every questionnaire is the sex of the respondent. Here, for simplicity, just two categories were recorded,
"M"and"F". (You usually need more categories for survey data; either way, you use a factor to store the categorical data.)Recording the sex with the abbreviations
"M"and"F"can be convenient if you are collecting data with pen and paper, but it can introduce confusion when analyzing the data. At that point, you will often want to change the factor levels to"Male"and"Female"instead of"M"and"F"for clarity.Watch out: the order with which you assign the levels is important. If you type
levels(factor_survey_vector), you'll see that it outputs[1] "F" "M". If you don't specify the levels of the factor when creating the vector, R will automatically assign them alphabetically. To correctly map"F"to"Female"and"M"to"Male", the levels should be set toc("Female", "Male"), in this order.Instructions
survey_vector. You should usefactor_survey_vectorin the next instruction.factor_survey_vectortoc("Female", "Male"). Mind the order of the vector elements here.Summarizing a Factor
After finishing this course, one of your favorite functions in R will be summary(). This will give you a quick overview of the contents of a variable:
Going back to our survey, you would like to know how many
"Male"responses you have in your study, and how many"Female"responses. Thesummary()function gives you the answer to this question.Instructions
Ask a
summary()of thesurvey_vectorandfactor_survey_vector. Interpret the results of both vectors. Are they both equally useful in this case?Ordered Factors
Since
"Male"and"Female"are unordered (or nominal) factor levels, R returns a warning message, telling you that the greater than operator is not meaningful. As seen before, R attaches an equal value to the levels for such factors.But this is not always the case! Sometimes you will also deal with factors that do have a natural ordering between its categories. If this is the case, we have to make sure that we pass this information to R...
Let us say that you are leading a research team of five data analysts and that you want to evaluate their performance. To do this, you track their speed, evaluate each analyst as
"slow","medium"or"fast", and save the results inspeed_vector.Instructions
As a first step, assign
speed_vectora vector with 5 entries, one for each analyst. Each entry should be either"slow","medium", or"fast". Use the list below:No need to specify these are factors yet.
If you want to learn more from this course, here is the link.
Check out out Factors in R Tutorial.