📖 Scenario: Imagine you are organizing a small survey about favorite fruits. You want to store the answers in a way that clearly shows the choices are categories, not numbers.
🎯 Goal: You will create a factor in R to represent categorical data and see how R treats it differently from normal text data.
📋 What You'll Learn
Create a character vector with fruit names
Convert the character vector to a factor
Check the structure of the factor
Print the factor to see its levels
💡 Why This Matters
🌍 Real World
Factors are used in surveys, experiments, and any data where values belong to fixed groups like colors, brands, or types.
💼 Career
Understanding factors is important for data analysis and statistics jobs where categorical data is common.
Progress0 / 4 steps
1
Create a character vector of fruits
Create a character vector called fruits with these exact values: "apple", "banana", "apple", "orange", "banana"
R Programming
Hint
Use the c() function to combine the fruit names into a vector.
2
Convert the character vector to a factor
Create a factor called fruit_factor by converting the fruits vector using the factor() function
R Programming
Hint
Use factor(fruits) to create the factor.
3
Check the structure of the factor
Use the str() function on fruit_factor to see its internal structure
R Programming
Hint
The str() function shows the internal structure including levels.
4
Print the factor and its levels
Print fruit_factor and then print its levels using levels(fruit_factor)
R Programming
Hint
Use print() to display the factor and its levels.
Practice
(1/5)
1. Why are factors used in R to represent categorical data?
easy
A. Because factors automatically sort data alphabetically
B. Because factors store only numeric data
C. Because factors group data into categories with levels
D. Because factors convert data into dates
Solution
Step 1: Understand what factors do in R
Factors group data into distinct categories called levels, which represent categorical data.
Step 2: Compare with other options
Factors do not store only numeric data, nor do they convert data into dates or automatically sort alphabetically.
Final Answer:
Because factors group data into categories with levels -> Option C
Quick Check:
Factors = Categories [OK]
Hint: Factors group data into categories, not just numbers or text [OK]
Common Mistakes:
Thinking factors only store numbers
Assuming factors sort data automatically
Confusing factors with date conversion
2. Which of the following is the correct way to create a factor in R from a character vector colors <- c('red', 'blue', 'red')?
easy
A. factor(colors)
B. as.numeric(colors)
C. sort(colors)
D. levels(colors)
Solution
Step 1: Identify the function to create a factor
The function factor() converts a vector into a factor with levels representing categories.
Step 2: Check other options
as.numeric() converts to numbers, sort() sorts the vector, and levels() retrieves levels but does not create a factor.
Final Answer:
factor(colors) -> Option A
Quick Check:
Create factor with factor() [OK]
Hint: Use factor() to convert vectors to categorical data [OK]
Common Mistakes:
Using as.numeric() instead of factor()
Trying to sort instead of factor conversion
Confusing levels() as a creation function
3. What will be the output of the following R code?
colors <- c('red', 'blue', 'red')
f <- factor(colors)
levels(f)
medium
A. ["red", "blue"]
B. ["blue", "red"]
C. ["red"]
D. ["blue"]
Solution
Step 1: Create factor and check levels
The factor f is created from colors which has values 'red' and 'blue'. Levels are sorted alphabetically by default.
Step 2: Determine levels order
Levels will be ["blue", "red"] because R sorts factor levels alphabetically unless specified otherwise.
Final Answer:
["blue", "red"] -> Option B
Quick Check:
Factor levels sorted alphabetically [OK]
Hint: Factor levels are alphabetical by default [OK]
Common Mistakes:
Assuming levels keep original order
Confusing factor values with levels
Expecting only one level
4. Identify the error in this R code that tries to create an ordered factor:
C. levels() assignment should use factor() argument, not after
D. No error, code is correct
Solution
Step 1: Trace the code execution
factor(sizes, ordered=TRUE) creates an ordered factor with default alphabetical levels. Then levels(f) <- c('small', 'medium', 'large') reassigns levels in the desired order, which is valid.
Step 2: Verify no errors
Assigning levels after creation works for ordered factors and sets the order as listed. The code runs without error.
Final Answer:
No error, code is correct -> Option D
Quick Check:
levels(f) <- works on ordered factors [OK]
Hint: levels() <- assignment works after factor creation [OK]
Common Mistakes:
Thinking levels must only be set inside factor()
Believing ordered factors cannot have character levels
Assuming levels assignment fails after creation
5. You have a vector responses <- c('low', 'high', 'medium', 'low'). How do factors help when analyzing this data as categorical with a natural order?
hard
A. Factors allow defining levels with order: low < medium < high
B. Factors convert all responses to numeric values automatically
C. Factors remove duplicate values from the vector
D. Factors sort the vector alphabetically by default
Solution
Step 1: Understand natural order in categorical data
Responses like 'low', 'medium', 'high' have a natural order that factors can represent by setting ordered levels.
Step 2: How factors help analysis
By creating an ordered factor with levels c('low', 'medium', 'high'), R can compare and sort these categories meaningfully.
Final Answer:
Factors allow defining levels with order: low < medium < high -> Option A
Quick Check:
Ordered factors represent natural category order [OK]
Hint: Use ordered factors to keep category sequence [OK]
Common Mistakes:
Assuming factors convert to numbers automatically
Thinking factors remove duplicates
Confusing alphabetical sorting with natural order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.