How to change the order of levels of a factor in R?
In R programming language, factors are used to represent categorical data by uniquely identifying the elements from the given vector. It will return the levels of the unique elements when factor function is applied. In this article we are going to discuss how to change the levels of the factor.
We can get the levels of the vector using factor() function itself
- vector: vector with categorical data
- levels(): specifies the levels
Returns: vector elements with levels
If we want to change the order of the levels, we can specify the levels parameter in the factor() function
levels parameter can accept the vector elements and within this, the order of levels can be passed as a vector.
Given below are various implementations for this approach: