Bird
Raised Fist0
R Programmingprogramming~10 mins

Ordered factors in R Programming - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an ordered factor from the vector 'sizes'.

R Programming
sizes <- c("small", "medium", "large")
ordered_sizes <- factor(sizes, levels = c("small", "medium", "large"), ordered = [1])
Drag options to blanks, or click blank then click option'
A"TRUE"
BFALSE
C1
DTRUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using FALSE instead of TRUE for the 'ordered' argument.
Passing 'TRUE' as a string instead of a logical value.
2fill in blank
medium

Complete the code to check if 'ordered_sizes' is an ordered factor.

R Programming
is.ordered([1])
Drag options to blanks, or click blank then click option'
Afactor
Bsizes
Cordered_sizes
Dlevels
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the original vector instead of the factor.
Passing the function name 'factor' instead of a variable.
3fill in blank
hard

Fix the error in the code to create an ordered factor with levels 'low', 'medium', 'high'.

R Programming
levels <- c("low", "medium", "high")
values <- c("medium", "high", "low")
factor_values <- factor(values, levels = levels, ordered = [1])
Drag options to blanks, or click blank then click option'
ATRUE
B1
C"TRUE"
DFALSE
Attempts:
3 left
💡 Hint
Common Mistakes
Using FALSE or a string instead of TRUE.
Passing numeric 1 instead of logical TRUE.
4fill in blank
hard

Fill both blanks to create an ordered factor with levels 'beginner', 'intermediate', 'advanced' and check if it is ordered.

R Programming
levels <- c("beginner", "intermediate", "advanced")
skills <- c("advanced", "beginner", "intermediate")
skill_factor <- factor(skills, levels = levels, ordered = [1])
check <- is.ordered([2])
Drag options to blanks, or click blank then click option'
ATRUE
BFALSE
Cskills
Dskill_factor
Attempts:
3 left
💡 Hint
Common Mistakes
Using FALSE for the ordered argument.
Checking the original vector instead of the factor.
5fill in blank
hard

Fill all three blanks to create an ordered factor from 'grades', set levels, and check if it is ordered.

R Programming
grades <- c("B", "A", "C")
levels <- c("C", [1], "A")
ordered_grades <- factor(grades, levels = levels, ordered = [2])
result <- is.ordered([3])
Drag options to blanks, or click blank then click option'
A"B"
BTRUE
Cordered_grades
DFALSE
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving out the middle level in levels.
Using FALSE for ordered argument.
Checking the original vector instead of the factor.

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

  1. Step 1: Understand what ordered factors do

    Ordered factors keep categories in a specific order defined by the user.
  2. Step 2: Recognize the comparison feature

    They allow comparisons like greater than or less than between categories.
  3. Final Answer:

    To keep categories in a specific order and allow comparisons like greater than or less than -> Option B
  4. 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

  1. Step 1: Check the function and parameters

    Using factor() with ordered=TRUE and specifying levels sets the order.
  2. Step 2: Verify the levels order

    The levels must be in ascending order: "low", "medium", "high".
  3. Final Answer:

    f <- factor(c("low", "high", "medium"), ordered=TRUE, levels=c("low", "medium", "high")) -> Option A
  4. 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

  1. Step 1: Understand the factor levels and order

    The levels are "small" < "medium" < "large".
  2. Step 2: Compare each element to "medium"

    "small" > "medium" is FALSE, "medium" > "medium" is FALSE, "large" > "medium" is TRUE.
  3. Final Answer:

    [FALSE FALSE TRUE] -> Option C
  4. 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:
levels <- c("low", "medium", "high")
f <- factor(c("low", "high", "medium"), ordered=TRUE, levels=levels)
medium
A. The ordered=TRUE argument is missing
B. The levels vector is not in alphabetical order
C. The factor vector contains values not in levels
D. The variable name 'levels' conflicts with the function argument name

Solution

  1. Step 1: Check variable and argument names

    Using 'levels' as a variable name shadows the function argument 'levels'.
  2. Step 2: Understand the conflict

    This can cause unexpected behavior or errors because R confuses the variable with the argument.
  3. Final Answer:

    The variable name 'levels' conflicts with the function argument name -> Option D
  4. 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"?
hard
A. response <- factor(c("Agree", "Disagree", "Neutral"), ordered=TRUE, levels=c("Disagree", "Neutral", "Agree")) response >= "Neutral"
B. response <- ordered(c("Agree", "Disagree", "Neutral"), levels=c("Agree", "Neutral", "Disagree")) response >= "Neutral"
C. response <- factor(c("Agree", "Disagree", "Neutral"), levels=c("Disagree", "Neutral", "Agree")) response >= "Neutral"
D. response <- ordered(c("Agree", "Disagree", "Neutral")) response >= "Neutral"

Solution

  1. Step 1: Set levels in correct order

    The levels must be "Disagree" < "Neutral" < "Agree" to reflect the survey order.
  2. Step 2: Create ordered factor and compare

    Using factor(..., ordered=TRUE, levels=...) creates the ordered factor. Then, response >= "Neutral" checks if responses are at least "Neutral".
  3. Final Answer:

    response <- factor(c("Agree", "Disagree", "Neutral"), ordered=TRUE, levels=c("Disagree", "Neutral", "Agree")) response >= "Neutral" -> Option A
  4. Quick Check:

    Correct order and comparison with ordered factor [OK]
Hint: Set levels in order and use ordered=TRUE for comparisons [OK]
Common Mistakes:
  • Wrong levels order
  • Missing ordered=TRUE
  • Using ordered() without levels
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard