Complete the code to create a factor from the vector 'colors'.
colors <- c("red", "blue", "green", "blue") factor_colors <- [1](colors)
The factor() function creates a factor from a vector in R.
Complete the code to create a factor with specified levels.
colors <- c("red", "blue", "green", "blue") factor_colors <- factor(colors, levels = [1])
The levels argument expects a character vector of the factor levels.
Fix the error in the code to create a factor from 'sizes'.
sizes <- c("small", "medium", "large", "medium") factor_sizes <- factor([1])
The variable name is sizes. Using it directly creates the factor.
Fill both blanks to create an ordered factor with levels from small to large.
sizes <- c("small", "medium", "large", "medium") factor_sizes <- factor(sizes, levels = [1], ordered = [2])
Levels must be in order from smallest to largest, and ordered = TRUE makes the factor ordered.
Fill all three blanks to create a factor from 'fruits' with levels and labels.
fruits <- c("a", "b", "c", "a") factor_fruits <- factor(fruits, levels = [1], labels = [2], ordered = [3])
Levels are the original codes, labels are the descriptive names, and ordered is FALSE for unordered factors.
