Recall & Review
beginner
What is an ordered factor in R?
An ordered factor in R is a categorical variable where the categories have a natural order or ranking, like 'low', 'medium', 'high'. It helps R understand that the levels have a sequence.
Click to reveal answer
beginner
How do you create an ordered factor in R?
Use the
factor() function with the argument ordered = TRUE. For example: factor(c('low', 'medium', 'high'), ordered = TRUE, levels = c('low', 'medium', 'high')).Click to reveal answer
intermediate
Why specify the levels when creating an ordered factor?
Specifying levels sets the order of categories explicitly. Without it, R might order levels alphabetically, which can be wrong for ordered data.
Click to reveal answer
intermediate
What happens if you compare two ordered factors in R?
R compares their order based on the levels. For example, 'low' < 'medium' is TRUE if levels are set as low < medium < high.
Click to reveal answer
intermediate
Can you convert an unordered factor to an ordered factor?
Yes, by using
factor() again with ordered = TRUE and specifying the correct levels order.Click to reveal answer
Which argument makes a factor ordered in R?
✗ Incorrect
The argument
ordered = TRUE tells R to treat the factor as ordered.What is the default order of levels if you don't specify them in a factor?
✗ Incorrect
By default, R orders factor levels alphabetically unless you specify otherwise.
If
f is an ordered factor with levels low < medium < high, what is the result of f[1] < f[2] when f[1] = 'low' and f[2] = 'high'?✗ Incorrect
Since 'low' is less than 'high' in the order, the comparison returns TRUE.
How do you check if a factor is ordered in R?
✗ Incorrect
The function
is.ordered() returns TRUE if the factor is ordered.What happens if you compare unordered factors with < or > in R?
✗ Incorrect
Comparing unordered factors with < or > returns NA (with a warning) because '<' is not meaningful for factors.
Explain what an ordered factor is and why it is useful in R.
Think about categories like 'small', 'medium', 'large' that have a clear order.
You got /4 concepts.
Describe how to create an ordered factor from a character vector in R.
Remember to tell R the order of categories explicitly.
You got /4 concepts.