0
0
R Programmingprogramming~15 mins

Releveling factors in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
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 level
Print 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
1
Create the factor variable
Create a factor variable called fruits with these exact values in order: "Apple", "Banana", "Cherry", "Date", "Banana"
R Programming
Need a hint?

Use the factor() function with a vector of fruit names.

2
Set the new first level
Create a variable called new_first_level and set it to the string "Banana"
R Programming
Need a hint?

Just assign the string "Banana" to the variable new_first_level.

3
Relevel the factor
Use the relevel() function to create a new factor called fruits_releveled by releveling fruits with the first level set to new_first_level
R Programming
Need a hint?

Use relevel(fruits, ref = new_first_level) to change the first level.

4
Print the new levels
Print the levels of fruits_releveled using the levels() function
R Programming
Need a hint?

Use print(levels(fruits_releveled)) to see the new order of levels.