To reorder factor levels in R, you can use the factor()
function with the levels
argument to specify the desired order of levels. This is useful when you need to change the order in which levels appear in a factor.
In this example,
days
which contains the values 'Monday'
, 'Tuesday'
, 'Wednesday'
, 'Thursday'
, 'Friday'
, 'Saturday'
, and 'Sunday'
. This vector represents days of the week in order.factor()
function to convert the days
vector into a factor. We assign the result to a variable named days_factor
. The factor()
function automatically identifies the unique levels of the vector.factor()
function again to reorder the levels of the days_factor
by specifying the levels in a new order: c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
. This new order starts the week from Sunday.days_factor
to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.days <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
days_factor <- factor(days)
days_factor <- factor(days_factor, levels = c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'))
print(days_factor)
[1] Monday Tuesday Wednesday Thursday Friday Saturday Sunday Levels: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
In this example,
sizes
which contains the values 'small'
, 'medium'
, 'large'
, and 'extra large'
. This vector represents different size categories.factor()
function to convert the sizes
vector into a factor. We assign the result to a variable named sizes_factor
. The factor()
function automatically identifies the unique levels of the vector.factor()
function again to reorder the levels of the sizes_factor
by specifying the levels in a new order: c('extra large', 'large', 'medium', 'small')
. This new order represents a descending size order.sizes_factor
to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.sizes <- c('small', 'medium', 'large', 'extra large')
sizes_factor <- factor(sizes)
sizes_factor <- factor(sizes_factor, levels = c('extra large', 'large', 'medium', 'small'))
print(sizes_factor)
[1] small medium large extra large Levels: extra large large medium small
In this example,
fruits
which contains the values 'apple'
, 'banana'
, 'cherry'
, and 'date'
. This vector represents different types of fruits.factor()
function to convert the fruits
vector into a factor. We assign the result to a variable named fruits_factor
. The factor()
function automatically identifies the unique levels of the vector.factor()
function again to reorder the levels of the fruits_factor
by specifying the levels in a new order: c('date', 'cherry', 'banana', 'apple')
. This new order represents the fruits in alphabetical order starting from the end.fruits_factor
to the console to see the reordered factor levels. This allows us to verify the new order of the factor levels.fruits <- c('apple', 'banana', 'cherry', 'date')
fruits_factor <- factor(fruits)
fruits_factor <- factor(fruits_factor, levels = c('date', 'cherry', 'banana', 'apple'))
print(fruits_factor)
[1] apple banana cherry date Levels: date cherry banana apple
In this tutorial, we learned How to Reorder Factor Levels in R language with well detailed examples.