0
0
R Programmingprogramming~15 mins

Why factors represent categorical data in R Programming - See It in Action

Choose your learning style9 modes available
Why factors represent categorical data
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print() to display the factor and its levels.