0
0
R Programmingprogramming~20 mins

Releveling factors in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1] "medium" "low" "high"
B[1] "low" "medium" "high"
C[1] "high" "medium" "low"
D[1] "medium" "high" "low"
Attempts:
2 left
💡 Hint
The relevel function changes the reference level to the one you specify, moving it to the first position.
Predict Output
intermediate
2: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)
A[1] "apple" "banana" "cherry"
B[1] "banana" "apple" "cherry"
CError in relevel(f, ref = "orange"): ref level 'orange' not found in factor levels
D[1] "orange" "apple" "banana" "cherry"
Attempts:
2 left
💡 Hint
Check if the reference level exists in the factor levels before releveling.
🔧 Debug
advanced
2: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)
ABecause relevel returns a new factor and does not modify 'f' in place.
BBecause 'green' is already the first level, so no change occurs.
CBecause the factor 'f' is not a valid factor object.
DBecause relevel only works on numeric factors.
Attempts:
2 left
💡 Hint
Check if the result of relevel is assigned back to a variable.
Predict Output
advanced
2: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)
A[1] "medium" "small" "large"
B[1] "large" "small" "medium"
C[1] "small" "medium" "large"
D[1] "small" "large" "medium"
Attempts:
2 left
💡 Hint
Each relevel call resets the first level to the specified reference.
🧠 Conceptual
expert
3:00remaining
Effect of relevel on model contrasts
In R, why is releveling a factor important before fitting a linear model?
ABecause it increases the number of factor levels automatically.
BBecause it changes the baseline level, affecting the interpretation of model coefficients.
CBecause it converts factors to numeric variables for the model.
DBecause it removes unused factor levels from the dataset.
Attempts:
2 left
💡 Hint
Think about how the first level of a factor is used in regression models.