0
0
R Programmingprogramming~10 mins

Factor levels in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Factor levels
Create vector with values
Convert vector to factor
Assign levels automatically
Use levels for ordering or grouping
Access or modify levels if needed
Use factor in analysis or plots
Start with a vector, convert it to a factor which assigns levels automatically, then use or modify these levels for grouping or ordering.
Execution Sample
R Programming
x <- c("apple", "banana", "apple", "cherry")
f <- factor(x)
levels(f)
print(f)
Create a character vector, convert it to a factor, check its levels, and print the factor showing values as levels.
Execution Table
StepActionVariableValue/ResultNotes
1Create vector xx["apple", "banana", "apple", "cherry"]Vector with repeated fruits
2Convert x to factor ffFactor with levels: apple, banana, cherryLevels assigned alphabetically by default
3Check levels of flevels(f)["apple", "banana", "cherry"]Shows unique sorted levels
4Print factor ff[1, 2, 1, 3]Values shown as factor level codes
5EndExecution complete
💡 All steps executed; factor created with levels assigned automatically.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xundefined["apple", "banana", "apple", "cherry"]["apple", "banana", "apple", "cherry"]["apple", "banana", "apple", "cherry"]
fundefinedundefinedFactor with levels: apple, banana, cherryFactor with levels: apple, banana, cherry
Key Moments - 2 Insights
Why does the factor have levels sorted alphabetically, not in the order of appearance?
By default, R assigns factor levels in alphabetical order as shown in step 2 and 3 of the execution_table.
What does printing a factor show compared to printing a character vector?
Printing a factor shows the underlying integer codes representing the factor levels, not just raw strings, as seen in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what are the levels of factor f after step 2?
A["apple", "cherry", "banana"]
B["banana", "apple", "cherry"]
C["apple", "banana", "cherry"]
D["cherry", "banana", "apple"]
💡 Hint
Check the 'Value/Result' column in row for step 2 in execution_table.
At which step is the factor f created from vector x?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when conversion to factor happens.
If the vector x had values c("banana", "apple", "banana"), how would the levels be ordered by default?
A["apple", "banana"]
B["banana", "apple"]
C["banana"]
D["apple"]
💡 Hint
Remember factor levels are sorted alphabetically by default as shown in execution_table step 2.
Concept Snapshot
Factor levels in R:
- Create a vector of values
- Convert to factor with factor()
- Levels assigned alphabetically by default
- Use levels() to see or modify levels
- Factors help group and order categorical data
Full Transcript
This example shows how to create a factor in R from a character vector. First, a vector x with fruit names is created. Then, x is converted to a factor f. R automatically assigns levels to f in alphabetical order: apple, banana, cherry. Using levels(f) shows these levels. Printing f displays the underlying integer codes representing the factor levels. Factors are useful for grouping and ordering categories in data analysis.