Recall & Review
beginner
What is a factor in R?
A factor in R is a data structure used to represent categorical data. It stores the data as integer codes with corresponding labels called levels.
Click to reveal answer
beginner
How do you create a factor from a character vector in R?
Use the function
factor() and pass the character vector as an argument. For example: factor(c("apple", "banana", "apple")).Click to reveal answer
intermediate
What does the
levels argument do in factor()?The
levels argument sets the possible categories (levels) of the factor. It controls the order and which categories are included, even if not present in the data.Click to reveal answer
beginner
How can you check the levels of a factor in R?
Use the
levels() function on the factor variable. It returns a character vector of the factor's levels.Click to reveal answer
intermediate
What happens if you create a factor with numeric values?
R treats the numeric values as categories, not numbers. The factor stores them as levels, so arithmetic operations won't work directly on factors.
Click to reveal answer
Which function is used to create a factor in R?
✗ Incorrect
The
factor() function creates a factor from a vector.What does the
levels argument in factor() control?✗ Incorrect
The
levels argument sets which categories exist and their order.If you create a factor from the vector
c("red", "blue", "red"), how many levels will it have?✗ Incorrect
There are two unique categories: "red" and "blue".
What will
levels(factor(c(1, 2, 1))) return?✗ Incorrect
Levels are stored as character strings representing the unique values.
Can you perform arithmetic directly on factors?
✗ Incorrect
Factors represent categories, so arithmetic operations do not work directly.
Explain how to create a factor in R and why factors are useful.
Think about how R stores categories and why that helps with data analysis.
You got /4 concepts.
Describe what happens when you create a factor from numeric values and how it differs from numeric vectors.
Consider how factors treat numbers as labels, not quantities.
You got /4 concepts.