Challenge - 5 Problems
Ordered Factor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of ordered factor comparison
What is the output of this R code snippet?
R Programming
levels <- c("low", "medium", "high") ord_factor <- factor(c("medium", "low", "high"), levels = levels, ordered = TRUE) ord_factor[1] > ord_factor[2]
Attempts:
2 left
💡 Hint
Remember that ordered factors allow comparison based on their levels.
✗ Incorrect
The factor is ordered with levels low < medium < high. Comparing 'medium' > 'low' returns TRUE.
❓ Predict Output
intermediate1:30remaining
Number of levels in an ordered factor
How many levels does this ordered factor have?
R Programming
ord_factor <- ordered(c("small", "medium", "large", "medium"), levels = c("small", "medium", "large")) length(levels(ord_factor))
Attempts:
2 left
💡 Hint
Levels are defined explicitly in the factor creation.
✗ Incorrect
The factor has three levels: small, medium, and large.
🔧 Debug
advanced2:00remaining
Why does this ordered factor comparison fail?
What error does this code produce and why?
R Programming
f1 <- factor(c("a", "b"), ordered = TRUE) f2 <- factor(c("a", "b"), levels = c("b", "a"), ordered = TRUE) f1 > f2
Attempts:
2 left
💡 Hint
Check if the factors have the same levels and order.
✗ Incorrect
Even though both factors have the same values, their levels are set in different orders ('a' < 'b' vs. 'b' < 'a'), causing comparison to fail.
🧠 Conceptual
advanced2:00remaining
Effect of changing levels order on ordered factor comparison
Given this code, what is the output of the comparison?
R Programming
ord_factor <- ordered(c("low", "medium", "high"), levels = c("high", "medium", "low")) ord_factor[1] < ord_factor[3]
Attempts:
2 left
💡 Hint
The order of levels defines the comparison order.
✗ Incorrect
Levels are reversed: high < medium < low. 'low' is last, 'high' is first, so 'low' < 'high' is FALSE.
📝 Syntax
expert2:30remaining
Identify the syntax error in ordered factor creation
Which option contains the correct syntax to create an ordered factor with levels "small", "medium", "large"?
Attempts:
2 left
💡 Hint
Check the correct argument names and their order.
✗ Incorrect
Option A correctly uses factor() with levels and ordered=TRUE. Option A correctly uses ordered() with named levels argument. Option A has argument order wrong but still works in R. Option A misses argument name for levels causing error.