📖 Scenario: Imagine you have survey data where people's favorite fruits are recorded as categories. You want to change the order of these categories to make "Banana" the first choice for analysis.
🎯 Goal: You will create a factor variable with fruit names, then relevel it so that "Banana" is the first level.
📋 What You'll Learn
Create a factor variable called fruits with the values: "Apple", "Banana", "Cherry", "Date", "Banana"
Create a variable called new_first_level and set it to "Banana"
Use the relevel() function to create a new factor called fruits_releveled with new_first_level as the first level
Print the levels of fruits_releveled to show the new order
💡 Why This Matters
🌍 Real World
Releveling factors is useful when you want to change the reference category in surveys, experiments, or any categorical data analysis.
💼 Career
Data analysts and statisticians often need to reorder factor levels to control how models interpret categorical variables or how plots display categories.
Progress0 / 4 steps
1
Create the factor variable
Create a factor variable called fruits with these exact values in order: "Apple", "Banana", "Cherry", "Date", "Banana"
R Programming
Hint
Use the factor() function with a vector of fruit names.
2
Set the new first level
Create a variable called new_first_level and set it to the string "Banana"
R Programming
Hint
Just assign the string "Banana" to the variable new_first_level.
3
Relevel the factor
Use the relevel() function to create a new factor called fruits_releveled by releveling fruits with the first level set to new_first_level
R Programming
Hint
Use relevel(fruits, ref = new_first_level) to change the first level.
4
Print the new levels
Print the levels of fruits_releveled using the levels() function
R Programming
Hint
Use print(levels(fruits_releveled)) to see the new order of levels.
Practice
(1/5)
1. What does the relevel() function do in R when working with factors?
easy
A. It changes the reference level of a factor to a specified category.
B. It converts a factor into a numeric vector.
C. It sorts the levels of a factor alphabetically.
D. It removes unused levels from a factor.
Solution
Step 1: Understand the purpose of relevel()
The relevel() function is used to set a new baseline or reference level for a factor in R.
Step 2: Identify what changing the reference level means
Changing the reference level means selecting which category is treated as the baseline for comparisons or modeling.
Final Answer:
It changes the reference level of a factor to a specified category. -> Option A
Quick Check:
relevel() sets new baseline [OK]
Hint: Remember: relevel() sets the baseline category [OK]
Common Mistakes:
Thinking relevel() converts factor to numeric
Confusing relevel() with sorting levels
Assuming relevel() removes unused levels
2. Which of the following is the correct syntax to set the reference level of a factor f to "Control" using relevel()?
easy
A. relevel(f, reference = "Control")
B. relevel(f, level = "Control")
C. relevel(f, base = "Control")
D. relevel(f, ref = "Control")
Solution
Step 1: Recall the argument name for reference level in relevel()
The correct argument to specify the new reference level is ref.
Step 2: Match the syntax with the argument
Only relevel(f, ref = "Control") uses the correct argument name and syntax.
Final Answer:
relevel(f, ref = "Control") -> Option D
Quick Check:
Argument for reference is ref = "Control" [OK]
Hint: Use ref= to set new baseline in relevel() [OK]
A. ["A", "B", "C"] unchanged because "D" is ignored.
B. ["D", "A", "B", "C"] as new levels with "D" added.
C. An error because "D" is not a level in the factor.
D. Levels reordered alphabetically without error.
Solution
Step 1: Check if "D" is a level in factor f
Levels are "A", "B", "C"; "D" is not present.
Step 2: Behavior of relevel() with invalid ref
relevel() throws an error if the reference level is not in the factor levels.
Final Answer:
An error because "D" is not a level in the factor. -> Option C
Quick Check:
Invalid ref level causes error [OK]
Hint: Reference must exist in levels or error occurs [OK]
Common Mistakes:
Assuming relevel adds new levels automatically
Expecting no error and unchanged levels
Thinking levels reorder alphabetically silently
5. You have a factor f with levels "Red", "Green", "Blue". You want to set "Green" as the reference level and then create a summary table counting each level's occurrences using the summary() function. Which code correctly does this?
hard
A. f_new <- relevel(f, ref = "Green")
table(f_new)
B. f_new <- relevel(f, ref = "Green")
summary(f_new)
C. f_new <- relevel(f, ref = "Green")
table(f)
D. f_new <- relevel(f, ref = "Green")
summary(f)
Solution
Step 1: Relevel factor to set "Green" as reference
Use f_new <- relevel(f, ref = "Green") to create a new factor with "Green" first.
Step 2: Use summary() on the re-leveled factor
Calling summary(f_new) counts occurrences of each level in the updated factor.
Final Answer:
f_new <- relevel(f, ref = "Green")
summary(f_new) -> Option B
Quick Check:
summary() on re-leveled factor gives counts [OK]
Hint: Use summary() on re-leveled factor for counts [OK]
Common Mistakes:
Using summary() on original factor after relevel
Using table() on original factor instead of new
Not assigning relevel result to new variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.