Factors in R

Learn about factor objects in R and their use cases.

The factor data type

Factors are beneficial when we store data as categorical variables and construct a hierarchical structure among values. The classical use case for factor data is when values in a dataset are duplicated many times, and there are a limited number of unique values.

Here is an example:

c('Small', 'Big',' Medium', 'Small', 'Big', 'Small', 'Big', 'Medium', 'Big') 
# or 

Create factors

The factor() function converts containers into factor objects. This function determines unique values as distinct levels (categories). A factor object does not allow the addition of elements outside the established categories. The levels() function displays the levels, which are the unique values that exist in the object.

Get hands-on with 1200+ tech skills courses.