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.