0
0
R Programmingprogramming~15 mins

Factor levels in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
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 levels
Print 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
1
Create the character vector of fruits
Create a character vector called fruits with these exact values: "apple", "banana", "cherry", "banana", "apple"
R Programming
Need a hint?

Use the c() function to combine the fruit names into a vector.

2
Create the vector of factor levels
Create a character vector called fruit_levels with these exact values: "apple", "banana", "cherry", "date"
R Programming
Need a hint?

Use c() again to list the levels in the order you want.

3
Create the factor with specified levels
Create a factor called fruit_factor from the vector fruits using the levels from fruit_levels
R Programming
Need a hint?

Use the factor() function with the levels argument.

4
Print the factor to see the levels
Print the variable fruit_factor to display the factor with its levels
R Programming
Need a hint?

Use print(fruit_factor) to show the factor and its levels.