Releveling Factors in R
📖 Scenario: Imagine you have survey data where people's favorite fruits are recorded as categories. You want to change the order of these categories to make "Banana" the first choice for analysis.
🎯 Goal: You will create a factor variable with fruit names, then relevel it so that "Banana" is the first level.
📋 What You'll Learn
Create a factor variable called
fruits with the values: "Apple", "Banana", "Cherry", "Date", "Banana"Create a variable called
new_first_level and set it to "Banana"Use the
relevel() function to create a new factor called fruits_releveled with new_first_level as the first levelPrint the levels of
fruits_releveled to show the new order💡 Why This Matters
🌍 Real World
Releveling factors is useful when you want to change the reference category in surveys, experiments, or any categorical data analysis.
💼 Career
Data analysts and statisticians often need to reorder factor levels to control how models interpret categorical variables or how plots display categories.
Progress0 / 4 steps