📖 Scenario: You work in a small company that collects survey data about customer satisfaction. The survey answers are stored as text, but you want to organize them better for analysis.
🎯 Goal: You will create a factor variable in R from a character vector of survey answers. This will help group and analyze the answers easily.
📋 What You'll Learn
Create a character vector with exact survey answers
Create a factor variable from the character vector
Specify the levels of the factor in a given order
Print the factor variable to see the result
💡 Why This Matters
🌍 Real World
Survey data often comes as text answers. Converting them to factors helps organize and analyze the data efficiently.
💼 Career
Data analysts and statisticians use factors to handle categorical data in R for reports and visualizations.
Progress0 / 4 steps
1
Create the survey answers vector
Create a character vector called answers with these exact values: "Yes", "No", "Maybe", "Yes", "No".
R Programming
Hint
Use the c() function to combine the answers into a vector.
2
Create the factor variable
Create a factor variable called answers_factor from the answers vector using the factor() function.
R Programming
Hint
Use factor(answers) to convert the character vector to a factor.
3
Specify the factor levels order
Create the factor variable answers_factor again from answers, but this time specify the levels in this exact order: "No", "Maybe", "Yes".
R Programming
Hint
Use the levels argument inside factor() to set the order.
4
Print the factor variable
Print the answers_factor variable to display its values and levels.
R Programming
Hint
Use print(answers_factor) to show the factor values and their levels.
Practice
(1/5)
1. What is the main purpose of creating a factor in R?
easy
A. To represent categorical data with specific levels
B. To perform mathematical calculations faster
C. To store large numeric datasets efficiently
D. To create complex graphical plots automatically
Solution
Step 1: Understand factor usage in R
Factors are used to represent categorical data with fixed categories called levels.
Step 2: Compare options with factor purpose
Only To represent categorical data with specific levels correctly describes factors as categorical data with levels.
Final Answer:
To represent categorical data with specific levels -> Option A
Quick Check:
Factor = categorical data with levels [OK]
Hint: Factors are for categories, not numbers or plots [OK]
Common Mistakes:
Thinking factors speed up math calculations
Confusing factors with numeric vectors
Assuming factors create plots automatically
2. Which of the following is the correct way to create a factor from a character vector colors <- c('red', 'blue', 'red')?
easy
A. matrix(colors)
B. factor(colors)
C. list(colors)
D. as.numeric(colors)
Solution
Step 1: Identify function to create factor
The function factor() converts a vector into a factor with levels.
Step 2: Check other options
as.numeric() converts to numbers, list() and matrix() create different structures, not factors.
Final Answer:
factor(colors) -> Option B
Quick Check:
Use factor() to create factors [OK]
Hint: Use factor() to convert vectors to factors [OK]
Common Mistakes:
Using as.numeric() instead of factor()
Confusing list() or matrix() with factor creation
Not wrapping vector inside factor()
3. What will be the output of the following R code?
D. The levels argument is missing 'green', causing NA values
Solution
Step 1: Check levels vs data values
The vector has 'green' but levels only include 'red' and 'blue'. This causes 'green' to become NA in the factor.
Step 2: Understand summary() behavior
summary() counts factor levels including NAs, so missing 'green' in levels causes unexpected NA counts.
Final Answer:
The levels argument is missing 'green', causing NA values -> Option D
Quick Check:
Levels must include all categories to avoid NA [OK]
Hint: Include all categories in levels to avoid NA [OK]
Common Mistakes:
Thinking factor() ignores missing levels
Believing summary() can't handle factors
Assuming colors must be numeric
5. You have a character vector responses <- c('yes', 'no', 'yes', 'maybe', 'no'). You want to create an ordered factor with levels no < maybe < yes. Which code correctly creates this ordered factor?
hard
A. factor(responses, levels = c('no', 'maybe', 'yes'), ordered = TRUE)
B. factor(responses, levels = c('yes', 'no', 'maybe'), ordered = TRUE)
C. factor(responses, ordered = TRUE)
D. factor(responses, levels = c('no', 'maybe', 'yes'))
Solution
Step 1: Set levels in desired order
Levels must be set as c('no', 'maybe', 'yes') to reflect the order no < maybe < yes.
Step 2: Make factor ordered
Use ordered = TRUE to create an ordered factor reflecting this order.