Bird
Raised Fist0
R Programmingprogramming~20 mins

Releveling factors in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Factor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after releveling a factor
What is the output of this R code after releveling the factor?
R Programming
f <- factor(c('low', 'medium', 'high', 'medium', 'low'))
f <- relevel(f, ref = 'medium')
levels(f)
A[1] "medium" "low" "high"
B[1] "low" "medium" "high"
C[1] "high" "medium" "low"
D[1] "medium" "high" "low"
Attempts:
2 left
💡 Hint
The relevel function changes the reference level to the one you specify, moving it to the first position.
❓ Predict Output
intermediate
2:00remaining
Factor levels after releveling with a non-existent level
What happens when you try to relevel a factor with a reference level that does not exist?
R Programming
f <- factor(c('apple', 'banana', 'cherry'))
f <- relevel(f, ref = 'orange')
levels(f)
A[1] "apple" "banana" "cherry"
B[1] "banana" "apple" "cherry"
CError in relevel(f, ref = "orange"): ref level 'orange' not found in factor levels
D[1] "orange" "apple" "banana" "cherry"
Attempts:
2 left
💡 Hint
Check if the reference level exists in the factor levels before releveling.
🔧 Debug
advanced
2:00remaining
Why does this relevel code not change the factor levels?
Consider this code snippet. Why does the factor levels remain unchanged after releveling?
R Programming
f <- factor(c('red', 'green', 'blue'))
relevel(f, ref = 'green')
levels(f)
ABecause relevel returns a new factor and does not modify 'f' in place.
BBecause 'green' is already the first level, so no change occurs.
CBecause the factor 'f' is not a valid factor object.
DBecause relevel only works on numeric factors.
Attempts:
2 left
💡 Hint
Check if the result of relevel is assigned back to a variable.
❓ Predict Output
advanced
2:00remaining
Levels order after multiple relevel calls
What is the final order of levels after these relevel calls?
R Programming
f <- factor(c('small', 'medium', 'large'))
f <- relevel(f, ref = 'large')
f <- relevel(f, ref = 'small')
levels(f)
A[1] "medium" "small" "large"
B[1] "large" "small" "medium"
C[1] "small" "medium" "large"
D[1] "small" "large" "medium"
Attempts:
2 left
💡 Hint
Each relevel call resets the first level to the specified reference.
🧠 Conceptual
expert
3:00remaining
Effect of relevel on model contrasts
In R, why is releveling a factor important before fitting a linear model?
ABecause it increases the number of factor levels automatically.
BBecause it changes the baseline level, affecting the interpretation of model coefficients.
CBecause it converts factors to numeric variables for the model.
DBecause it removes unused factor levels from the dataset.
Attempts:
2 left
💡 Hint
Think about how the first level of a factor is used in regression models.

Practice

(1/5)
1. What does the relevel() function do in R when working with factors?
easy
A. It changes the reference level of a factor to a specified category.
B. It converts a factor into a numeric vector.
C. It sorts the levels of a factor alphabetically.
D. It removes unused levels from a factor.

Solution

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

    The relevel() function is used to set a new baseline or reference level for a factor in R.
  2. Step 2: Identify what changing the reference level means

    Changing the reference level means selecting which category is treated as the baseline for comparisons or modeling.
  3. Final Answer:

    It changes the reference level of a factor to a specified category. -> Option A
  4. Quick Check:

    relevel() sets new baseline [OK]
Hint: Remember: relevel() sets the baseline category [OK]
Common Mistakes:
  • Thinking relevel() converts factor to numeric
  • Confusing relevel() with sorting levels
  • Assuming relevel() removes unused levels
2. Which of the following is the correct syntax to set the reference level of a factor f to "Control" using relevel()?
easy
A. relevel(f, reference = "Control")
B. relevel(f, level = "Control")
C. relevel(f, base = "Control")
D. relevel(f, ref = "Control")

Solution

  1. Step 1: Recall the argument name for reference level in relevel()

    The correct argument to specify the new reference level is ref.
  2. Step 2: Match the syntax with the argument

    Only relevel(f, ref = "Control") uses the correct argument name and syntax.
  3. Final Answer:

    relevel(f, ref = "Control") -> Option D
  4. Quick Check:

    Argument for reference is ref = "Control" [OK]
Hint: Use ref= to set new baseline in relevel() [OK]
Common Mistakes:
  • Using incorrect argument names like level or base
  • Forgetting quotes around the reference level
  • Using assignment instead of argument syntax
3. Given the code:
f <- factor(c("Low", "Medium", "High", "Low"))
f_new <- relevel(f, ref = "High")
levels(f_new)

What is the output of levels(f_new)?
medium
A. ["High", "Low", "Medium"]
B. ["Low", "Medium", "High"]
C. ["Medium", "Low", "High"]
D. ["High", "Medium", "Low"]

Solution

  1. Step 1: Understand original factor levels

    Original levels are in alphabetical order: "High", "Low", "Medium".
  2. Step 2: Effect of relevel(f, ref = "High")

    Relevel moves "High" to be the first (reference) level, keeping others in original order after it.
  3. Final Answer:

    ["High", "Low", "Medium"] -> Option A
  4. Quick Check:

    Reference level first = ["High", "Low", "Medium"] [OK]
Hint: Reference level appears first in levels() output [OK]
Common Mistakes:
  • Assuming levels reorder alphabetically after relevel
  • Confusing order of levels after relevel
  • Expecting levels to be unchanged
4. Consider this code snippet:
f <- factor(c("A", "B", "C"))
f_new <- relevel(f, ref = "D")
levels(f_new)

What will happen when this code runs?
medium
A. ["A", "B", "C"] unchanged because "D" is ignored.
B. ["D", "A", "B", "C"] as new levels with "D" added.
C. An error because "D" is not a level in the factor.
D. Levels reordered alphabetically without error.

Solution

  1. Step 1: Check if "D" is a level in factor f

    Levels are "A", "B", "C"; "D" is not present.
  2. Step 2: Behavior of relevel() with invalid ref

    relevel() throws an error if the reference level is not in the factor levels.
  3. Final Answer:

    An error because "D" is not a level in the factor. -> Option C
  4. Quick Check:

    Invalid ref level causes error [OK]
Hint: Reference must exist in levels or error occurs [OK]
Common Mistakes:
  • Assuming relevel adds new levels automatically
  • Expecting no error and unchanged levels
  • Thinking levels reorder alphabetically silently
5. You have a factor f with levels "Red", "Green", "Blue". You want to set "Green" as the reference level and then create a summary table counting each level's occurrences using the summary() function. Which code correctly does this?
hard
A. f_new <- relevel(f, ref = "Green") table(f_new)
B. f_new <- relevel(f, ref = "Green") summary(f_new)
C. f_new <- relevel(f, ref = "Green") table(f)
D. f_new <- relevel(f, ref = "Green") summary(f)

Solution

  1. Step 1: Relevel factor to set "Green" as reference

    Use f_new <- relevel(f, ref = "Green") to create a new factor with "Green" first.
  2. Step 2: Use summary() on the re-leveled factor

    Calling summary(f_new) counts occurrences of each level in the updated factor.
  3. Final Answer:

    f_new <- relevel(f, ref = "Green") summary(f_new) -> Option B
  4. Quick Check:

    summary() on re-leveled factor gives counts [OK]
Hint: Use summary() on re-leveled factor for counts [OK]
Common Mistakes:
  • Using summary() on original factor after relevel
  • Using table() on original factor instead of new
  • Not assigning relevel result to new variable
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