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 level count?
Consider the following R code that creates a factor and counts its levels.
What will be the output of
What will be the output of
table(f)?R Programming
f <- factor(c("apple", "banana", "apple", "cherry", "banana", "banana")) table(f)
Attempts:
2 left
💡 Hint
Remember that
table() counts how many times each factor level appears.✗ Incorrect
The factor
f has levels apple, banana, and cherry. The vector has apple twice, banana three times, and cherry once. So table(f) counts these occurrences.❓ Predict Output
intermediate2: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"))
Attempts:
2 left
💡 Hint
Check the counts from
table(f) and the colors vector length.✗ Incorrect
The factor has levels red, blue, green with counts 2, 3, and 1. The colors vector matches these levels in order, so the barplot shows 3 bars with correct colors.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Changing levels directly does not reorder factor values; use
factor(..., levels=...) instead.✗ Incorrect
Assigning new levels directly changes the labels but does not reorder the underlying factor codes, so the factor values become mismatched with levels. This does not cause an error but leads to incorrect factor values.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Use
ordered() or factor(..., ordered=TRUE) with correct levels order.✗ Incorrect
Option D uses
ordered() with levels in correct ascending order, creating an ordered factor. Option D is valid but less idiomatic. Option D misses levels so order is default. Option D reverses levels order.🚀 Application
expert3: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")
Attempts:
2 left
💡 Hint
Remember that geom_bar with stat="identity" plots one bar per row, not per factor level.
✗ Incorrect
With stat="identity", ggplot plots one bar per row in the data frame, so 6 bars appear. The factor levels control x-axis order but do not reduce bars. Level D has no data, so no bar for D.