A factor in R is a data type used to represent categorical variables. It stores categories as levels and is useful for statistical modeling and plotting.
Click to reveal answer
beginner
How do you create a factor from a character vector in R?
Use the function factor(). For example: factor(c('red', 'blue', 'red')) creates a factor with levels 'blue' and 'red'.
Click to reveal answer
intermediate
Why are factors important in plotting with ggplot2?
Factors control the order and grouping of categories in plots. They help ggplot2 know how to display categorical data on axes or in colors.
Click to reveal answer
intermediate
How can you change the order of levels in a factor?
Use factor(x, levels = c(...)) to specify the order of levels. This changes how categories appear in analysis and plots.
Click to reveal answer
beginner
What happens if you plot a numeric variable as a factor?
The numeric values are treated as categories, not numbers. The plot will show discrete groups instead of a continuous scale.
Click to reveal answer
Which R function converts a character vector to a factor?
Aas.character()
Bfactor()
Cas.numeric()
Dlevels()
✗ Incorrect
The function factor() converts character vectors to factors.
What does the levels argument in factor() do?
AChanges factor to character
BConverts factor to numeric
CRemoves unused categories
DSets the order of categories
✗ Incorrect
The levels argument sets the order of categories in the factor.
In ggplot2, how does a factor affect the x-axis?
AShows continuous numeric scale
BRemoves the axis
CShows categories as discrete groups
DChanges color of points
✗ Incorrect
Factors make ggplot2 display categories as discrete groups on the axis.
If you plot a numeric variable as a factor, what will you see?
ADiscrete groups for each number
BContinuous line plot
CError message
DNo plot
✗ Incorrect
Plotting numeric data as a factor treats numbers as categories, showing discrete groups.
How do you check the levels of a factor variable in R?
Alevels()
Bclass()
Cstr()
Dsummary()
✗ Incorrect
The levels() function shows the categories of a factor.
Explain what a factor is in R and why it is useful for analysis and plotting.
Think about how categories are stored and used in graphs.
You got /5 concepts.
Describe how you would change the order of categories in a factor and why this might matter in a plot.
Consider how the order affects what you see on the graph.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using a factor in R when analyzing data?
easy
A. To convert text data into categories for easier analysis and plotting
B. To perform mathematical calculations on numeric data
C. To store large datasets efficiently
D. To create complex machine learning models
Solution
Step 1: Understand what factors represent
Factors in R are used to represent categorical data, turning text into categories.
Step 2: Recognize the benefit of factors
Using factors helps in organizing data for analysis and plotting by grouping similar values.
Final Answer:
To convert text data into categories for easier analysis and plotting -> Option A
Quick Check:
Factors = Categories [OK]
Hint: Factors group text into categories for analysis and plots [OK]
Common Mistakes:
Thinking factors perform calculations
Confusing factors with numeric vectors
Assuming factors store data efficiently
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(x, order = c("Low", "Medium", "High"))
B. factor(x, levels = c("Low", "Medium", "High"))
C. factor(x, labels = c("Low", "Medium", "High"))
D. factor(x, sort = c("Low", "Medium", "High"))
Solution
Step 1: Recall factor syntax for setting levels
The correct argument to set the order of categories is levels.
Step 2: Check the options
Only factor(x, levels = c("Low", "Medium", "High")) uses levels correctly to order the factor levels.
Final Answer:
factor(x, levels = c("Low", "Medium", "High")) -> Option B
Quick Check:
Use levels= to order categories [OK]
Hint: Use levels= to set factor order, not order= or sort= [OK]
Common Mistakes:
Using order= instead of levels=
Confusing labels= with levels=
Trying to sort levels with sort= argument
3. Consider the following R code:
x <- c("apple", "banana", "apple", "cherry")
f <- factor(x, levels = c("banana", "apple", "cherry"))
summary(f)
What will be the output of summary(f)?
medium
A. banana: 1 apple: 2 cherry: 1
B. apple: 2 banana: 1 cherry: 1
C. apple: 2 cherry: 1 banana: 1
D. cherry: 1 banana: 1 apple: 2
Solution
Step 1: Understand factor levels order
The factor levels are set as "banana", "apple", "cherry" which determines the order in summary output.
Step 2: Count occurrences of each level
In x, "banana" appears once, "apple" twice, and "cherry" once.
Final Answer:
banana: 1 apple: 2 cherry: 1 -> Option A
Quick Check:
Summary shows counts in levels order [OK]
Hint: Summary counts follow factor levels order [OK]
Common Mistakes:
Assuming summary sorts alphabetically
Ignoring levels order in summary
Counting values incorrectly
4. The following R code is intended to create a factor with levels "Small", "Medium", "Large" and plot a bar chart. However, the plot shows the levels in alphabetical order instead of the specified order.
A. The factor levels are correct, but barplot plots in alphabetical order by default
B. The barplot function ignores factor levels and sorts alphabetically
C. The factor was not created with the correct levels argument
D. The table function converts factor to character and loses order
Solution
Step 1: Check factor creation syntax
The factor is created with levels specified correctly, so this is not the issue.
Step 2: Understand barplot and table behavior
The table() function converts the factor to a named vector which may lose the factor level order, causing barplot to plot in alphabetical order.
Step 3: Identify common mistake
Using table() directly on factor can lose order; using barplot with factor levels preserved requires careful handling.
Final Answer:
The table function converts factor to character and loses order -> Option D
Quick Check:
Use methods preserving factor order for plotting [OK]
Hint: Always set levels when creating factor to control plot order [OK]
Common Mistakes:
Assuming barplot ignores factor levels
Not setting levels argument in factor()
Using character vector instead of factor in table()
5. You have a dataset with a column rating containing values "Poor", "Average", "Good", and "Excellent". You want to create a factor that orders these levels from worst to best and then plot a bar chart showing the counts in this order. Which R code correctly achieves this?
hard
A. rating_factor <- factor(rating, levels = sort(unique(rating)))
barplot(table(rating_factor))
B. rating_factor <- factor(rating)
barplot(table(rating_factor))