A factor in R is a data structure used to represent categorical data. It stores the data as integer codes with corresponding character labels called levels.
Click to reveal answer
beginner
How do you create a factor with specific levels in R?
Use the factor() function with the levels argument. For example: factor(c("low", "high", "medium"), levels = c("low", "medium", "high")) creates a factor with levels ordered as low, medium, high.
Click to reveal answer
intermediate
What happens if you assign a value not in the levels to a factor?
R will convert that value to NA (missing value) because it is not recognized as one of the factor's predefined levels.
Click to reveal answer
beginner
How can you see the levels of a factor variable in R?
Use the levels() function. For example, levels(my_factor) returns a character vector of the factor's levels.
Click to reveal answer
intermediate
Why is the order of factor levels important?
The order of factor levels affects how R orders and compares the categories. For example, in plots or summary statistics, the order controls the display and sorting.
Click to reveal answer
What does the levels() function return when used on a factor?
AThe data type of the factor
BThe numeric codes of the factor
CThe length of the factor
DA character vector of the factor's categories
✗ Incorrect
The levels() function returns the character vector of all categories (levels) defined for the factor.
What happens if you create a factor with levels = c("low", "medium") but include "high" in the data?
A"high" becomes NA because it's not in the levels
B"high" is automatically added to levels
CAn error is thrown
D"high" is ignored silently
✗ Incorrect
Values not in the specified levels are converted to NA in factors.
How do you create an ordered factor in R?
AUse factor() with ordered = TRUE
BUse levels() function
CUse as.character()
DUse numeric()
✗ Incorrect
Setting ordered = TRUE in factor() creates an ordered factor where levels have a meaningful order.
Why might you want to set the order of factor levels explicitly?
ATo convert factors to numeric
BTo speed up calculations
CTo control the order of categories in plots and summaries
DTo remove missing values
✗ Incorrect
Explicit order controls how categories appear in outputs like plots and tables.
Which function converts a character vector to a factor in R?
Afactor()
BBoth A and B
Clevels()
Das.factor()
✗ Incorrect
Both factor() and as.factor() convert character vectors to factors.
Explain what factor levels are and why they matter in R.
Think about how R stores categories and how order affects results.
You got /4 concepts.
Describe how to create a factor with custom levels and what happens if data contains values outside those levels.
Consider the factor() function and the levels parameter.
You got /3 concepts.
Practice
(1/5)
1. What does the levels() function do when applied to a factor in R?
easy
A. It changes the factor to a character vector.
B. It converts the factor to numeric values.
C. It shows the categories (levels) of the factor variable.
D. It removes unused levels from the factor.
Solution
Step 1: Understand the purpose of levels()
The levels() function in R returns the categories or labels assigned to a factor variable.
Step 2: Differentiate from other functions
Functions like as.numeric() convert factors to numbers, and droplevels() removes unused levels, but levels() only shows the current levels.
Final Answer:
It shows the categories (levels) of the factor variable. -> Option C
Quick Check:
levels() = shows categories [OK]
Hint: Remember: levels() lists categories, not changes data type [OK]
Common Mistakes:
Confusing levels() with as.numeric()
Thinking levels() removes unused categories
Assuming levels() converts factor to character
2. Which of the following is the correct way to create a factor with levels ordered as "Low", "Medium", "High" in R?
easy
A. factor(c("Low", "High", "Medium"), levels = c("Low", "Medium", "High"))
B. factor(c("Low", "High", "Medium"), levels = c("High", "Medium", "Low"))
C. factor(c("Low", "High", "Medium"))
D. factor(c("Low", "High", "Medium"), ordered = FALSE)
Solution
Step 1: Understand factor creation with levels
To set a specific order of categories, use the levels argument with the desired order.
Step 2: Check the options
factor(c("Low", "High", "Medium"), levels = c("Low", "Medium", "High")) sets levels as "Low", "Medium", "High" which matches the required order. factor(c("Low", "High", "Medium"), levels = c("High", "Medium", "Low")) reverses the order, C uses default alphabetical order, and D does not specify levels.
A. The factor must be numeric before changing levels.
B. levels() cannot be assigned directly like this.
C. No error; code runs fine and shows updated levels.
D. Assigning fewer levels than existing causes mismatch error.
Solution
Step 1: Understand levels assignment
Assigning new levels directly replaces the old levels. The new levels vector must match the length of existing levels.
Step 2: Check the code
Original factor has 3 levels: "blue", "green", "red" (alphabetical order). The code tries to assign only 2 levels, causing a mismatch error.
Final Answer:
Assigning fewer levels than existing causes mismatch error. -> Option D
Quick Check:
levels replacement length must match [OK]
Hint: New levels vector length must equal old levels length [OK]
Common Mistakes:
Assigning fewer or more levels than original
Thinking levels() assignment always works
Ignoring alphabetical default order of levels
5. You have a factor f with levels c("small", "medium", "large"). You want to reorder the levels so that "large" comes first, then "small", then "medium". Which code correctly achieves this without changing the data values?
hard
A. f <- factor(f, levels = c("large", "small", "medium"))
B. levels(f) <- c("large", "small", "medium")
C. f <- factor(f, ordered = TRUE)
D. f <- factor(f, levels = sort(levels(f)))
Solution
Step 1: Understand reordering factor levels
To reorder levels without changing data, recreate the factor with the new levels order using the levels argument.
Step 2: Analyze options
f <- factor(f, levels = c("large", "small", "medium")) correctly sets new levels order. levels(f) <- c("large", "small", "medium") tries to assign levels directly, which can cause errors if lengths mismatch. f <- factor(f, ordered = TRUE) only makes factor ordered but does not reorder levels. f <- factor(f, levels = sort(levels(f))) sorts levels alphabetically, not custom order.
Final Answer:
f <- factor(f, levels = c("large", "small", "medium")) -> Option A
Quick Check:
Recreate factor with new levels order [OK]
Hint: Use factor() with levels= to reorder levels safely [OK]
Common Mistakes:
Assigning levels vector directly causing errors
Using ordered=TRUE without changing levels
Sorting levels instead of custom order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.