0
0
R Programmingprogramming~10 mins

Releveling factors in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Releveling factors
Start with factor variable
Choose new reference level
Apply relevel() function
Factor levels updated
Use re-leveled factor in analysis
This flow shows starting with a factor, choosing a new reference level, applying relevel(), and updating the factor levels.
Execution Sample
R Programming
f <- factor(c("low", "medium", "high", "medium"))
f_new <- relevel(f, ref = "medium")
levels(f_new)
This code creates a factor f, then relevels it to make "medium" the reference level, and shows the new levels.
Execution Table
StepActionInput/ConditionResult/Output
1Create factor fc("low", "medium", "high", "medium")f with levels: high, low, medium
2Call relevel(f, ref = "medium")f levels: high, low, mediumNew factor f_new with levels: medium, high, low
3Check levels(f_new)f_new["medium", "high", "low"]
4Use f_new in analysisf_new with new referenceReference level is now "medium"
💡 Releveling complete; factor levels reordered with new reference level.
Variable Tracker
VariableStartAfter relevelFinal
ffactor with levels: high, low, mediumunchangedunchanged
f_newnot definedfactor with levels: medium, high, lowfactor with levels: medium, high, low
Key Moments - 2 Insights
Why does the order of levels change after relevel()?
Because relevel() moves the specified reference level to the front, changing the order as shown in execution_table step 2.
Does relevel() change the original factor variable?
No, relevel() returns a new factor with reordered levels; the original factor remains unchanged as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the first level of f_new?
A"high"
B"low"
C"medium"
D"medium" and "low"
💡 Hint
Check the 'Result/Output' column at step 2 in execution_table.
At which step do we see the new order of levels for the re-leveled factor?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result/Output' columns in execution_table.
If we changed ref to "high" in relevel(), what would be the first level in f_new?
A"high"
B"low"
C"medium"
DNo change
💡 Hint
Relevel moves the chosen ref level to the front as shown in execution_table step 2.
Concept Snapshot
relevel(factor, ref = "level")
- Moves 'ref' level to be the first level
- Returns a new factor with reordered levels
- Original factor stays unchanged
- Useful to set baseline/reference in analysis
- Levels order affects model contrasts
Full Transcript
We start with a factor variable f with levels high, low, medium. We want to change the reference level to medium. Using relevel(f, ref = "medium"), we create a new factor f_new where medium is the first level. The original factor f remains unchanged. This is important because the first level is the reference in many analyses. The execution table shows each step: creating f, releveling to f_new, checking levels, and using f_new. The variable tracker confirms f is unchanged and f_new has the new order. Common confusions include why the order changes and whether the original factor changes. The visual quiz tests understanding of the new level order and effects of changing the reference level.