R Factors
A factor is a special data type for categorical variables — variables that belong to a fixed set of named groups. Examples include gender (Male/Female), education level (High School/Bachelor/Master/PhD), or product categories. Factors store the possible values efficiently and give them a defined order when needed.
Why Factors Exist
Without Factors: With Factors:
───────────────────────────────── ──────────────────────────────────────
size <- c("S","M","L","M","S","L") size <- factor(c("S","M","L","M","S","L"))
Just text — R doesn't know R knows "S", "M", "L" are the only
there are only 3 valid sizes valid sizes → more efficient + safer
Creating a Factor
# Unordered factor (nominal)
blood_type <- factor(c("A", "B", "O", "AB", "O", "A", "B"))
print(blood_type)
# [1] A B O AB O A B
# Levels: A AB B O
levels(blood_type) # "A" "AB" "B" "O"
nlevels(blood_type) # 4
Factor Structure Diagram
blood_type factor:
Values: A B O AB O A B
│ │ │ │ │ │ │
Stored: 1 3 4 2 4 1 3 (integer codes internally)
│ │ │ │ │ │ │
Levels: [1]=A [2]=AB [3]=B [4]=O
Factors store the category labels (levels) once, and then store integers internally for each value. This saves memory with large datasets.
Ordered Factors
edu <- factor(
c("Bachelor", "PhD", "Master", "High School", "Bachelor"),
levels = c("High School", "Bachelor", "Master", "PhD"),
ordered = TRUE
)
print(edu)
# [1] Bachelor PhD Master High School Bachelor
# Levels: High School < Bachelor < Master < PhD
edu[2] > edu[3] # PhD > Master → TRUE
edu[4] < edu[1] # High School < Bachelor → TRUE
Modifying Factors
# Rename levels
size <- factor(c("S", "M", "L", "M", "S"))
levels(size)[levels(size) == "S"] <- "Small"
levels(size)[levels(size) == "M"] <- "Medium"
levels(size)[levels(size) == "L"] <- "Large"
print(size)
# [1] Small Medium Large Medium Small
Counting Levels with table()
blood_type <- factor(c("A","B","O","AB","O","A","B","O","A"))
table(blood_type)
# blood_type
# A AB B O
# 3 1 2 3
Factors in Data Frames
df <- data.frame(
name = c("Riya", "Arjun", "Meena", "Dev"),
gender = factor(c("F", "M", "F", "M")),
grade = factor(c("A", "B", "A", "C"),
levels = c("C","B","A"), ordered = TRUE)
)
# Filter female students
df[df$gender == "F", ]
# Students with grade A or above
df[df$grade >= "A", ]
Converting Factors
f <- factor(c("3", "1", "2", "1"))
as.character(f) # "3" "1" "2" "1"
as.numeric(f) # 3 1 2 1 (careful: this uses internal codes sometimes)
# Safe way to convert factor of numbers to numeric:
as.numeric(as.character(f)) # always correct
Dropping Unused Levels
sizes <- factor(c("S", "M", "L", "XL"), levels = c("XS","S","M","L","XL"))
small_sizes <- sizes[sizes %in% c("S", "M")]
levels(small_sizes) # still has XS, L, XL (unused!)
droplevels(small_sizes) # removes unused levels → S, M only
Factors are critical for statistical modeling — regression functions, ANOVA, and plotting all treat factors specially. Using them correctly ensures your categorical data is handled with its proper structure rather than as plain text.
