Challenge - 5 Problems
Factor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this factor levels code?
Consider this R code that creates a factor variable. What will be the output of
levels(fruit)?R Programming
fruit <- factor(c("apple", "banana", "apple", "orange")) levels(fruit)
Attempts:
2 left
💡 Hint
Think about what unique categories the factor stores.
✗ Incorrect
Factors store unique categories as levels. Here, the unique fruits are apple, banana, and orange, so levels() returns all three.
🧠 Conceptual
intermediate2:00remaining
Why are factors used for categorical data in R?
Which of the following best explains why factors represent categorical data in R?
Attempts:
2 left
💡 Hint
Think about how factors internally represent categories.
✗ Incorrect
Factors store categorical data as integer codes with labels for each category. This helps R handle categories efficiently.
❓ Predict Output
advanced2:00remaining
What happens when you compare two factors with different levels?
What will be the output of this R code?
R Programming
f1 <- factor(c("low", "medium", "high"), levels = c("low", "medium", "high")) f2 <- factor(c("low", "medium", "high"), levels = c("high", "low", "medium")) f1 == f2
Attempts:
2 left
💡 Hint
Check how factor levels affect comparison.
✗ Incorrect
Even though the labels look the same, the underlying integer codes differ because the levels are ordered differently, so all comparisons return FALSE.
🔧 Debug
advanced2:00remaining
Why does this factor conversion produce unexpected results?
What is the output of this code and why?
R Programming
x <- c("10", "2", "30") f <- factor(x) as.numeric(f)
Attempts:
2 left
💡 Hint
Remember how factors store data internally.
✗ Incorrect
as.numeric on a factor returns the internal integer codes, not the original numbers. Here, the factor levels are sorted alphabetically, so "10" is level 1, "2" is level 2, "30" is level 3.
🚀 Application
expert2:00remaining
How many unique categories does this factor have after modification?
Given this R code, what is the number of unique categories in
f after the assignment?R Programming
f <- factor(c("red", "blue", "green")) levels(f) <- c("red", "blue", "yellow") f <- c(f, "yellow") length(levels(f))
Attempts:
2 left
💡 Hint
Think about how levels are changed and what happens when adding a new value.
✗ Incorrect
levels(f) <- c("red", "blue", "yellow") replaces the levels ("green" becomes NA). Adding "yellow" which is not a factor level causes an error.