0
0
R Programmingprogramming~20 mins

Ordered factors in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ordered Factor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]
AError: comparison not possible
B[1] FALSE
C[1] NA
D[1] TRUE
Attempts:
2 left
💡 Hint
Remember that ordered factors allow comparison based on their levels.
Predict Output
intermediate
1: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))
AError: levels not found
B3
C2
D4
Attempts:
2 left
💡 Hint
Levels are defined explicitly in the factor creation.
🔧 Debug
advanced
2: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
AError: comparison not possible between factors with different levels
B[1] TRUE TRUE
C[1] FALSE FALSE
DError: factors must have the same levels
Attempts:
2 left
💡 Hint
Check if the factors have the same levels and order.
🧠 Conceptual
advanced
2: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]
A[1] NA
B[1] TRUE
C[1] FALSE
DError: invalid comparison
Attempts:
2 left
💡 Hint
The order of levels defines the comparison order.
📝 Syntax
expert
2: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"?
Afactor(c("small", "medium", "large"), levels = c("small", "medium", "large"), ordered = TRUE)
Bfactor(c("small", "medium", "large"), ordered = TRUE, levels = c("small", "medium", "large"))
Cordered(c("small", "medium", "large"), c("small", "medium", "large"))
Dordered(c("small", "medium", "large"), levels = c("small", "medium", "large"))
Attempts:
2 left
💡 Hint
Check the correct argument names and their order.