R – Level Ordering of Factors

Factors are data objects used to categorize data and store it as levels. They can store a string as well as an integer. They represent columns as they have a limited number of unique values. Factors in R can be created using the factor() function. It takes a vector as input. c() function is used to create a vector with explicitly provided values. 

Example: 

R




x <- c("Pen", "Pencil", "Brush", "Pen",
        "Brush", "Brush", "Pencil", "Pencil")
 
print(x)
print(is.factor(x))
 
# Apply the factor function.
factor_x = factor(x)
levels(factor_x)


Output : 

[1] "Pen"    "Pencil" "Brush"  "Pen"    "Brush"  "Brush"  "Pencil" "Pencil"

[1] FALSE

[1] "Brush"  "Pen"    "Pencil"

In the above code, x is a vector with 8 elements. To convert it to a factor the function factor() is used. Here there are 8 factors and 3 levels. Levels are the unique elements in the data. It can be found using the levels() function. 

Level Ordering of Factors in R Programming

In this article, we will see the level ordering of factors in the R Programming Language.

Similar Reads

R – Level Ordering of Factors

Factors are data objects used to categorize data and store it as levels. They can store a string as well as an integer. They represent columns as they have a limited number of unique values. Factors in R can be created using the factor() function. It takes a vector as input. c() function is used to create a vector with explicitly provided values....

Ordering Factor Levels

...

Level ordering visualization in R

Ordered factors levels are an extension of factors. It arranges the levels in increasing order. We use two functions: factor() and argument ordered()....

Contact Us