Challenge - 5 Problems
Factor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after releveling a factor
What is the output of this R code after releveling the factor?
R Programming
f <- factor(c('low', 'medium', 'high', 'medium', 'low')) f <- relevel(f, ref = 'medium') levels(f)
Attempts:
2 left
💡 Hint
The relevel function changes the reference level to the one you specify, moving it to the first position.
✗ Incorrect
The relevel function sets the specified level as the first level in the factor levels. Here, 'medium' becomes the first level, followed by the others in their original order.
❓ Predict Output
intermediate2:00remaining
Factor levels after releveling with a non-existent level
What happens when you try to relevel a factor with a reference level that does not exist?
R Programming
f <- factor(c('apple', 'banana', 'cherry')) f <- relevel(f, ref = 'orange') levels(f)
Attempts:
2 left
💡 Hint
Check if the reference level exists in the factor levels before releveling.
✗ Incorrect
If the reference level is not found in the factor levels, relevel throws an error indicating the missing level.
🔧 Debug
advanced2:00remaining
Why does this relevel code not change the factor levels?
Consider this code snippet. Why does the factor levels remain unchanged after releveling?
R Programming
f <- factor(c('red', 'green', 'blue')) relevel(f, ref = 'green') levels(f)
Attempts:
2 left
💡 Hint
Check if the result of relevel is assigned back to a variable.
✗ Incorrect
The relevel function returns a new factor with reordered levels but does not change the original factor unless you assign it back.
❓ Predict Output
advanced2:00remaining
Levels order after multiple relevel calls
What is the final order of levels after these relevel calls?
R Programming
f <- factor(c('small', 'medium', 'large')) f <- relevel(f, ref = 'large') f <- relevel(f, ref = 'small') levels(f)
Attempts:
2 left
💡 Hint
Each relevel call resets the first level to the specified reference.
✗ Incorrect
The first relevel sets 'large' as first, then the second relevel sets 'small' as first, so final order starts with 'small'.
🧠 Conceptual
expert3:00remaining
Effect of relevel on model contrasts
In R, why is releveling a factor important before fitting a linear model?
Attempts:
2 left
💡 Hint
Think about how the first level of a factor is used in regression models.
✗ Incorrect
The first level of a factor is the baseline in model contrasts. Releveling changes which category is the baseline, thus changing coefficient interpretation.