Factors
In this page:
Factors
A factor represents categorical data with a fixed, known set of possible values (called levels), such as "low", "medium", "high". Factors are created with factor() and store the data more efficiently than plain strings while also enabling proper statistical treatment of categories. levels() lists the distinct categories a factor can take.
Note: Use factor(x, levels = c(...), ordered = TRUE) when the categories have a meaningful order, like small < medium < large.
Example: Factors
sizes <- factor(c("small", "large", "medium", "small"),
levels = c("small", "medium", "large"))
print(sizes)
print(levels(sizes))
print(table(sizes))
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: