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?
Afactor()
Bas.factor()
Clevels()
Dcategorize()
✗ Incorrect
The factor() function creates a factor from a vector.
What does the levels argument in factor() control?
AThe order and categories of the factor
BThe numeric values of the factor
CThe length of the vector
DThe data type of the vector
✗ 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?
A1
B3
C2
D0
✗ Incorrect
There are two unique categories: "red" and "blue".
What will levels(factor(c(1, 2, 1))) return?
AAn error
B[1, 2]
C["1", "2", "3"]
D["1", "2"]
✗ Incorrect
Levels are stored as character strings representing the unique values.
Can you perform arithmetic directly on factors?
AYes, factors behave like numbers
BNo, factors represent categories, not numbers
COnly if levels are numeric
DOnly with the <code>levels</code> argument
✗ 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.
Practice
(1/5)
1. What is the main purpose of creating a factor in R?
easy
A. To represent categorical data with specific levels
B. To perform mathematical calculations faster
C. To store large numeric datasets efficiently
D. To create complex graphical plots automatically
Solution
Step 1: Understand factor usage in R
Factors are used to represent categorical data with fixed categories called levels.
Step 2: Compare options with factor purpose
Only To represent categorical data with specific levels correctly describes factors as categorical data with levels.
Final Answer:
To represent categorical data with specific levels -> Option A
Quick Check:
Factor = categorical data with levels [OK]
Hint: Factors are for categories, not numbers or plots [OK]
Common Mistakes:
Thinking factors speed up math calculations
Confusing factors with numeric vectors
Assuming factors create plots automatically
2. Which of the following is the correct way to create a factor from a character vector colors <- c('red', 'blue', 'red')?
easy
A. matrix(colors)
B. factor(colors)
C. list(colors)
D. as.numeric(colors)
Solution
Step 1: Identify function to create factor
The function factor() converts a vector into a factor with levels.
Step 2: Check other options
as.numeric() converts to numbers, list() and matrix() create different structures, not factors.
Final Answer:
factor(colors) -> Option B
Quick Check:
Use factor() to create factors [OK]
Hint: Use factor() to convert vectors to factors [OK]
Common Mistakes:
Using as.numeric() instead of factor()
Confusing list() or matrix() with factor creation
Not wrapping vector inside factor()
3. What will be the output of the following R code?
D. The levels argument is missing 'green', causing NA values
Solution
Step 1: Check levels vs data values
The vector has 'green' but levels only include 'red' and 'blue'. This causes 'green' to become NA in the factor.
Step 2: Understand summary() behavior
summary() counts factor levels including NAs, so missing 'green' in levels causes unexpected NA counts.
Final Answer:
The levels argument is missing 'green', causing NA values -> Option D
Quick Check:
Levels must include all categories to avoid NA [OK]
Hint: Include all categories in levels to avoid NA [OK]
Common Mistakes:
Thinking factor() ignores missing levels
Believing summary() can't handle factors
Assuming colors must be numeric
5. You have a character vector responses <- c('yes', 'no', 'yes', 'maybe', 'no'). You want to create an ordered factor with levels no < maybe < yes. Which code correctly creates this ordered factor?
hard
A. factor(responses, levels = c('no', 'maybe', 'yes'), ordered = TRUE)
B. factor(responses, levels = c('yes', 'no', 'maybe'), ordered = TRUE)
C. factor(responses, ordered = TRUE)
D. factor(responses, levels = c('no', 'maybe', 'yes'))
Solution
Step 1: Set levels in desired order
Levels must be set as c('no', 'maybe', 'yes') to reflect the order no < maybe < yes.
Step 2: Make factor ordered
Use ordered = TRUE to create an ordered factor reflecting this order.