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?
Aordered = TRUE
Blevels = TRUE
Corder = TRUE
Drank = TRUE
✗ 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?
AReverse alphabetical order
BOrder of appearance
CRandom order
DAlphabetical order
✗ 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'?
AError
BTRUE
CFALSE
DNA
✗ 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?
AUse <code>is.ordered_factor()</code>
BUse <code>is.factor()</code>
CUse <code>is.ordered()</code>
D>edoc/<)(rotcaf_deredro.si>edoc< esU
✗ Incorrect
The function is.ordered() returns TRUE if the factor is ordered.
What happens if you compare unordered factors with < or > in R?
AIt returns NA
BIt compares alphabetically
CIt compares based on levels order
DIt gives an error
✗ 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.
Practice
(1/5)
1. What is the main purpose of creating an ordered factor in R?
easy
A. To store numeric data more efficiently
B. To keep categories in a specific order and allow comparisons like greater than or less than
C. To convert character strings into numeric values automatically
D. To remove duplicate values from a vector
Solution
Step 1: Understand what ordered factors do
Ordered factors keep categories in a specific order defined by the user.
Step 2: Recognize the comparison feature
They allow comparisons like greater than or less than between categories.
Final Answer:
To keep categories in a specific order and allow comparisons like greater than or less than -> Option B
Quick Check:
Ordered factors = ordered categories with comparison [OK]
Hint: Ordered factors let you compare categories by order [OK]
Common Mistakes:
Thinking ordered factors store numeric data
Confusing ordered factors with numeric conversion
Believing ordered factors remove duplicates
2. Which of the following is the correct way to create an ordered factor f with levels "low" < "medium" < "high" in R?
easy
A. f <- factor(c("low", "high", "medium"), ordered=TRUE, levels=c("low", "medium", "high"))
B. f <- ordered(c("low", "high", "medium"), levels=c("high", "medium", "low"))
C. f <- factor(c("low", "high", "medium"), levels=c("high", "medium", "low"), ordered=TRUE)
D. f <- ordered(c("low", "high", "medium"))
Solution
Step 1: Check the function and parameters
Using factor() with ordered=TRUE and specifying levels sets the order.
Step 2: Verify the levels order
The levels must be in ascending order: "low", "medium", "high".
Final Answer:
f <- factor(c("low", "high", "medium"), ordered=TRUE, levels=c("low", "medium", "high")) -> Option A
Quick Check:
Correct syntax with ordered=TRUE and levels order [OK]
Hint: Use factor(..., ordered=TRUE, levels=correct_order) [OK]
Common Mistakes:
Reversing the levels order
Using ordered() without levels
Setting ordered=TRUE but wrong levels order
3. What will be the output of the following R code?
f <- factor(c("small", "medium", "large"), ordered=TRUE, levels=c("small", "medium", "large"))
f > "medium"
medium
A. [FALSE FALSE FALSE]
B. [FALSE TRUE TRUE]
C. [FALSE FALSE TRUE]
D. [TRUE FALSE TRUE]
Solution
Step 1: Understand the factor levels and order
The levels are "small" < "medium" < "large".
Step 2: Compare each element to "medium"
"small" > "medium" is FALSE, "medium" > "medium" is FALSE, "large" > "medium" is TRUE.
Final Answer:
[FALSE FALSE TRUE] -> Option C
Quick Check:
Only "large" is greater than "medium" [OK]
Hint: Compare ordered factor elements to level with > operator [OK]
Common Mistakes:
Confusing the order of levels
Assuming equality returns TRUE
Mixing up TRUE/FALSE positions
4. Identify the error in this R code that tries to create an ordered factor:
C. The factor vector contains values not in levels
D. The variable name 'levels' conflicts with the function argument name
Solution
Step 1: Check variable and argument names
Using 'levels' as a variable name shadows the function argument 'levels'.
Step 2: Understand the conflict
This can cause unexpected behavior or errors because R confuses the variable with the argument.
Final Answer:
The variable name 'levels' conflicts with the function argument name -> Option D
Quick Check:
Variable name shadows argument name [OK]
Hint: Avoid naming variables 'levels' to prevent conflicts [OK]
Common Mistakes:
Ignoring variable name conflicts
Assuming alphabetical order is required
Missing ordered=TRUE argument
5. You have a survey with responses: "Agree", "Neutral", "Disagree". You want to create an ordered factor response where "Disagree" < "Neutral" < "Agree". Which R code correctly creates this ordered factor and allows you to check if a response is at least "Neutral"?