Bird
Raised Fist0
R Programmingprogramming~20 mins

Factor levels in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Factor Levels Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this factor levels code?
Consider the following R code that creates a factor and then changes its levels. What will be the output of levels(f)?
R Programming
f <- factor(c("low", "medium", "high", "medium", "low"))
levels(f) <- c("low", "medium", "high", "very high")
levels(f)
AError: number of levels differs from previous
B[1] "low" "medium" "high"
C[1] "low" "medium" "high" "very high" "extreme"
D[1] "low" "medium" "high" "very high"
Attempts:
2 left
💡 Hint
The number of new levels must match the number of original levels exactly.
❓ Predict Output
intermediate
2:00remaining
What is the output of this factor level drop code?
Given this R code, what will be the output of levels(f) after dropping unused levels?
R Programming
f <- factor(c("apple", "banana", "apple", "cherry"), levels = c("apple", "banana", "cherry", "date"))
f <- droplevels(f[f != "date"])
levels(f)
A[1] "apple" "banana" "cherry" "date"
BError: object 'f' not found
C[1] "apple" "banana"
D[1] "apple" "banana" "cherry"
Attempts:
2 left
💡 Hint
droplevels removes levels not present in the data.
🔧 Debug
advanced
2:00remaining
Why does this factor level assignment cause an error?
Examine the code below. Why does assigning new levels cause an error?
R Programming
f <- factor(c("a", "b", "c"))
levels(f) <- c("x", "y")
ABecause the new levels vector contains invalid characters
BBecause factor levels cannot be changed once created
CBecause the new levels vector has fewer elements than the original levels
DBecause the factor vector contains NA values
Attempts:
2 left
💡 Hint
The number of new levels must match the number of original levels.
❓ Predict Output
advanced
2:00remaining
What is the output of this factor releveling code?
What will be the output of levels(f) after running this code?
R Programming
f <- factor(c("small", "medium", "large"), ordered = TRUE)
f <- relevel(f, ref = "large")
levels(f)
A[1] "small" "medium" "large"
B[1] "large" "small" "medium"
C[1] "medium" "small" "large"
DError: object 'relevel' not found
Attempts:
2 left
💡 Hint
relevel moves the specified level to the front.
🧠 Conceptual
expert
2:00remaining
How many levels does this factor have after modification?
Given the code below, how many levels does the factor f have after execution?
R Programming
f <- factor(c("red", "green", "blue", "green"), levels = c("red", "green", "blue", "yellow"))
f <- f[f != "yellow"]
f <- droplevels(f)
length(levels(f))
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
droplevels removes unused levels after subsetting.

Practice

(1/5)
1. What does the levels() function do when applied to a factor in R?
easy
A. It changes the factor to a character vector.
B. It converts the factor to numeric values.
C. It shows the categories (levels) of the factor variable.
D. It removes unused levels from the factor.

Solution

  1. Step 1: Understand the purpose of levels()

    The levels() function in R returns the categories or labels assigned to a factor variable.
  2. Step 2: Differentiate from other functions

    Functions like as.numeric() convert factors to numbers, and droplevels() removes unused levels, but levels() only shows the current levels.
  3. Final Answer:

    It shows the categories (levels) of the factor variable. -> Option C
  4. Quick Check:

    levels() = shows categories [OK]
Hint: Remember: levels() lists categories, not changes data type [OK]
Common Mistakes:
  • Confusing levels() with as.numeric()
  • Thinking levels() removes unused categories
  • Assuming levels() converts factor to character
2. Which of the following is the correct way to create a factor with levels ordered as "Low", "Medium", "High" in R?
easy
A. factor(c("Low", "High", "Medium"), levels = c("Low", "Medium", "High"))
B. factor(c("Low", "High", "Medium"), levels = c("High", "Medium", "Low"))
C. factor(c("Low", "High", "Medium"))
D. factor(c("Low", "High", "Medium"), ordered = FALSE)

Solution

  1. Step 1: Understand factor creation with levels

    To set a specific order of categories, use the levels argument with the desired order.
  2. Step 2: Check the options

    factor(c("Low", "High", "Medium"), levels = c("Low", "Medium", "High")) sets levels as "Low", "Medium", "High" which matches the required order. factor(c("Low", "High", "Medium"), levels = c("High", "Medium", "Low")) reverses the order, C uses default alphabetical order, and D does not specify levels.
  3. Final Answer:

    factor(c("Low", "High", "Medium"), levels = c("Low", "Medium", "High")) -> Option A
  4. Quick Check:

    levels argument sets order [OK]
Hint: Use levels= to set order, not just values [OK]
Common Mistakes:
  • Not specifying levels leads to alphabetical order
  • Setting levels in wrong order
  • Ignoring levels argument
3. What will be the output of the following R code?
f <- factor(c("apple", "banana", "apple", "cherry"), levels = c("banana", "apple", "cherry"))
levels(f)
medium
A. [1] "apple" "banana" "cherry"
B. [1] "banana" "apple" "cherry"
C. [1] "apple" "cherry" "banana"
D. [1] "banana" "cherry" "apple"

Solution

  1. Step 1: Check the levels argument in factor()

    The factor is created with levels explicitly set as c("banana", "apple", "cherry"). This means the order of levels is exactly as given.
  2. Step 2: Output of levels(f)

    Calling levels(f) returns the levels in the order they were set, so it will print "banana", "apple", "cherry".
  3. Final Answer:

    [1] "banana" "apple" "cherry" -> Option B
  4. Quick Check:

    levels() returns set order [OK]
Hint: levels() shows levels in order set, not data order [OK]
Common Mistakes:
  • Assuming levels are sorted alphabetically by default
  • Confusing factor values with levels order
  • Expecting levels() to reorder automatically
4. Identify the error in the following R code snippet:
colors <- factor(c("red", "blue", "green"))
levels(colors) <- c("red", "blue")
levels(colors)
medium
A. The factor must be numeric before changing levels.
B. levels() cannot be assigned directly like this.
C. No error; code runs fine and shows updated levels.
D. Assigning fewer levels than existing causes mismatch error.

Solution

  1. Step 1: Understand levels assignment

    Assigning new levels directly replaces the old levels. The new levels vector must match the length of existing levels.
  2. Step 2: Check the code

    Original factor has 3 levels: "blue", "green", "red" (alphabetical order). The code tries to assign only 2 levels, causing a mismatch error.
  3. Final Answer:

    Assigning fewer levels than existing causes mismatch error. -> Option D
  4. Quick Check:

    levels replacement length must match [OK]
Hint: New levels vector length must equal old levels length [OK]
Common Mistakes:
  • Assigning fewer or more levels than original
  • Thinking levels() assignment always works
  • Ignoring alphabetical default order of levels
5. You have a factor f with levels c("small", "medium", "large"). You want to reorder the levels so that "large" comes first, then "small", then "medium". Which code correctly achieves this without changing the data values?
hard
A. f <- factor(f, levels = c("large", "small", "medium"))
B. levels(f) <- c("large", "small", "medium")
C. f <- factor(f, ordered = TRUE)
D. f <- factor(f, levels = sort(levels(f)))

Solution

  1. Step 1: Understand reordering factor levels

    To reorder levels without changing data, recreate the factor with the new levels order using the levels argument.
  2. Step 2: Analyze options

    f <- factor(f, levels = c("large", "small", "medium")) correctly sets new levels order. levels(f) <- c("large", "small", "medium") tries to assign levels directly, which can cause errors if lengths mismatch. f <- factor(f, ordered = TRUE) only makes factor ordered but does not reorder levels. f <- factor(f, levels = sort(levels(f))) sorts levels alphabetically, not custom order.
  3. Final Answer:

    f <- factor(f, levels = c("large", "small", "medium")) -> Option A
  4. Quick Check:

    Recreate factor with new levels order [OK]
Hint: Use factor() with levels= to reorder levels safely [OK]
Common Mistakes:
  • Assigning levels vector directly causing errors
  • Using ordered=TRUE without changing levels
  • Sorting levels instead of custom order
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