0
0
R Programmingprogramming~20 mins

Factor in analysis and plotting in R Programming - Practice Problems & Coding Challenges

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 level count?
Consider the following R code that creates a factor and counts its levels.
What will be the output of table(f)?
R Programming
f <- factor(c("apple", "banana", "apple", "cherry", "banana", "banana"))
table(f)
A
apple 2
banana 3
cherry 1
B
apple 3
banana 2
cherry 1
C
apple 2
banana 2
cherry 2
D
apple 1
banana 3
cherry 2
Attempts:
2 left
💡 Hint
Remember that table() counts how many times each factor level appears.
Predict Output
intermediate
2:00remaining
What does this plot show about factor levels?
Given this R code, what will the barplot display?
R Programming
f <- factor(c("red", "blue", "red", "green", "blue", "blue"))
barplot(table(f), col = c("red", "blue", "green"))
AA barplot with 3 bars: red=3, blue=2, green=1, colored incorrectly
BA barplot with 3 bars: red=2, blue=3, green=1, colored accordingly
CA barplot with 2 bars: red=2, blue=3, green missing
DAn error because colors do not match factor levels
Attempts:
2 left
💡 Hint
Check the counts from table(f) and the colors vector length.
🔧 Debug
advanced
2:00remaining
Why does this factor releveling code fail?
This code tries to reorder factor levels but causes an error. What is the error?
R Programming
f <- factor(c("small", "medium", "large"))
levels(f) <- c("large", "medium", "small")
print(f)
AIt changes the levels but does not reorder the factor values, so output is incorrect
BIt causes a warning but prints factor with new levels
CIt causes an error: 'replacement has length zero' because levels assignment is invalid
DIt silently fails and keeps original levels
Attempts:
2 left
💡 Hint
Changing levels directly does not reorder factor values; use factor(..., levels=...) instead.
📝 Syntax
advanced
2:00remaining
Which option correctly creates an ordered factor?
You want to create an ordered factor with levels: low < medium < high. Which code does this correctly?
Af <- factor(c("low", "high", "medium"), levels = c("low", "medium", "high"), ordered = TRUE)
Bf <- factor(c("low", "high", "medium"), levels = c("high", "medium", "low"), ordered = TRUE)
Cf <- factor(c("low", "high", "medium"), ordered = TRUE)
Df <- ordered(c("low", "high", "medium"), levels = c("low", "medium", "high"))
Attempts:
2 left
💡 Hint
Use ordered() or factor(..., ordered=TRUE) with correct levels order.
🚀 Application
expert
3:00remaining
How many bars will this ggplot show?
Given this R code using ggplot2, how many bars will appear in the plot?
R Programming
library(ggplot2)
data <- data.frame(
  category = factor(c("A", "B", "A", "C", "B", "B"), levels = c("A", "B", "C", "D")),
  value = c(5, 3, 6, 2, 4, 1)
)
ggplot(data, aes(x = category, y = value)) + geom_bar(stat = "identity")
A4 bars: one for each level A, B, C, D (D with height zero)
B3 bars: only for levels A, B, C present in data
C6 bars: one for each row in data
DError because level D has no data
Attempts:
2 left
💡 Hint
Remember that geom_bar with stat="identity" plots one bar per row, not per factor level.