0
0
R Programmingprogramming~20 mins

Why factors represent categorical data in R Programming - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factor Mastery Badge
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 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)
A[1] "apple" "banana" "orange"
B[1] "apple" "orange"
C[1] "banana" "orange"
D[1] "apple" "banana"
Attempts:
2 left
💡 Hint
Think about what unique categories the factor stores.
🧠 Conceptual
intermediate
2:00remaining
Why are factors used for categorical data in R?
Which of the following best explains why factors represent categorical data in R?
AFactors automatically sort data in alphabetical order without labels.
BFactors store only numeric data and cannot hold text values.
CFactors convert all data to character strings for display.
DFactors store data as integers with labels, making categories easy to manage and analyze.
Attempts:
2 left
💡 Hint
Think about how factors internally represent categories.
Predict Output
advanced
2: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
A[1] FALSE FALSE FALSE
B[1] TRUE TRUE TRUE
CError: factors with different levels cannot be compared
D[1] TRUE FALSE TRUE
Attempts:
2 left
💡 Hint
Check how factor levels affect comparison.
🔧 Debug
advanced
2: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)
A[1] 10 2 30
B[1] 1 2 3
CError: cannot convert factor to numeric directly
D[1] NA NA NA
Attempts:
2 left
💡 Hint
Remember how factors store data internally.
🚀 Application
expert
2: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))
A5
B4
CError: invalid factor level
D3
Attempts:
2 left
💡 Hint
Think about how levels are changed and what happens when adding a new value.