0
0
R Programmingprogramming~20 mins

Factor creation in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factor Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of factor levels with default ordering
What is the output of the following R code?
f <- factor(c("apple", "banana", "apple", "cherry"))
levels(f)
R Programming
f <- factor(c("apple", "banana", "apple", "cherry"))
levels(f)
A[1] "cherry" "banana" "apple"
B[1] "banana" "apple" "cherry"
C[1] "apple" "cherry" "banana"
D[1] "apple" "banana" "cherry"
Attempts:
2 left
💡 Hint
Think about how R orders factor levels by default.
Predict Output
intermediate
2:00remaining
Number of levels in a factor
What is the output of this R code?
f <- factor(c("red", "blue", "red", "green", "blue"))
length(levels(f))
R Programming
f <- factor(c("red", "blue", "red", "green", "blue"))
length(levels(f))
A1
B3
C5
D4
Attempts:
2 left
💡 Hint
Count unique values in the vector.
Predict Output
advanced
2:00remaining
Effect of specifying levels in factor creation
What is the output of this R code?
f <- factor(c("small", "medium", "large", "medium"), levels = c("large", "medium", "small"))
levels(f)
R Programming
f <- factor(c("small", "medium", "large", "medium"), levels = c("large", "medium", "small"))
levels(f)
A[1] "large" "medium" "small"
B[1] "small" "medium" "large"
C[1] "medium" "small" "large"
D[1] "large" "small" "medium"
Attempts:
2 left
💡 Hint
Levels are set explicitly in the factor function.
Predict Output
advanced
2:00remaining
Output of factor with unused levels
What is the output of this R code?
f <- factor(c("cat", "dog"), levels = c("cat", "dog", "mouse"))
length(levels(f))
R Programming
f <- factor(c("cat", "dog"), levels = c("cat", "dog", "mouse"))
length(levels(f))
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Levels include all specified, even if not used in data.
🧠 Conceptual
expert
2:30remaining
Behavior of factor when converting numeric vector
Consider this R code:
v <- c(10, 20, 10, 30)
f <- factor(v)
levels(f)

What is the output of levels(f)?
R Programming
v <- c(10, 20, 10, 30)
f <- factor(v)
levels(f)
A[1] "1" "2" "3"
B[1] 10 20 30
C[1] "10" "20" "30"
D[1] 10 20 30 40
Attempts:
2 left
💡 Hint
Factor levels are always character strings representing unique values.