Factor levels
📖 Scenario: You are working with survey data where people's favorite fruits are recorded as text. To analyze this data properly, you need to convert the fruit names into a factor with specific levels.
🎯 Goal: Create a factor variable from a character vector of fruits with specified levels, so the data is ready for analysis.
📋 What You'll Learn
Create a character vector called
fruits with the values "apple", "banana", "cherry", "banana", "apple"Create a vector called
fruit_levels with the values "apple", "banana", "cherry", "date"Create a factor called
fruit_factor from fruits using fruit_levels as the levelsPrint the
fruit_factor to show the factor with levels💡 Why This Matters
🌍 Real World
Factors are used in R to handle categorical data like survey answers, product categories, or any group labels. Setting levels helps control the order and possible values.
💼 Career
Data analysts and statisticians use factors to prepare data for analysis and visualization, ensuring categories are handled correctly.
Progress0 / 4 steps