In the R programming language, factor variables are variables that hold categorical data and store data in different levels.

Often you may want to change the order of the levels of a factor variable.

One of the easiest ways to do so is by using the **fct_relevel()** function from the **forcats** package in R, which is designed to perform this exact task.

The **fct_relevel()** function uses the following basic syntax:

**fct_relevel(.f, …, after = 0L)**

where:

**.f**: The name of a factor variable**…**: A function or character levels**after**: Specifies where the level should be placed after

The following examples show how to use this function in practice to change the levels of a factor variable.

**Note**: Before using the **fct_relevel()** function you may need to first install the **forcats** package by using the following syntax:

install.packages('forcats')

Once the **forcats** package is installed, you can use the **fct_relevel()** function to reorder factor levels without encountering any errors.

**Example 1: Move Factor Level to Front**

Suppose we create the following factor variable named **my_teams **that contains the names of various basketball teams:

#create factor variable my_teams <- factor(c('Mavs', 'Nets', 'Nets', 'Kings', 'Mavs', 'Heat'), levels=c('Mavs', 'Nets', 'Heat', 'Kings')) #view factor variable my_teams [1] Mavs Nets Nets Kings Mavs Heat Levels: Mavs Nets Heat Kings

We can see that the **my_teams** factor variable currently has the following order of levels:

**Mavs Nets Heat Kings**

Suppose instead that we would like to move the **Heat** to the first position of the factor levels.

We can use the **fct_level()** function with the following syntax to do so:

library(forcats) #create factor variable my_teams <- factor(c('Mavs', 'Nets', 'Nets', 'Kings', 'Mavs', 'Heat'), levels=c('Mavs', 'Nets', 'Heat', 'Kings')) #move 'Heat' to front of level order my_teams <- fct_relevel(my_teams, 'Heat') #view updated factor variable my_teams [1] Mavs Nets Nets Kings Mavs Heat Levels: Heat Mavs Nets Kings

Notice that the **Heat** has been moved to the first position of the factor levels while all other levels have remained in the same positions.

**Example 2: Move Factor Level After Specific Level**

Once again, suppose we create the following factor variable named **my_teams **that contains the names of various basketball teams:

#create factor variable my_teams <- factor(c('Mavs', 'Nets', 'Nets', 'Kings', 'Mavs', 'Heat'), levels=c('Mavs', 'Nets', 'Heat', 'Kings')) #view factor variable my_teams [1] Mavs Nets Nets Kings Mavs Heat Levels: Mavs Nets Heat Kings

Suppose that we would like to move the **Heat** to the position after **1** of the factor levels.

We can use the **fct_level()** function with the following syntax to do so:

library(forcats) #create factor variable my_teams <- factor(c('Mavs', 'Nets', 'Nets', 'Kings', 'Mavs', 'Heat'), levels=c('Mavs', 'Nets', 'Heat', 'Kings')) #move 'Heat' to after position 1 my_teams <- fct_relevel(my_teams, 'Heat') #view updated factor variable my_teams [1] Mavs Nets Nets Kings Mavs Heat Levels: Mavs Heat Nets Kings

Notice that **Heat** has been moved to the position directly after position **1** of the factor levels while all other levels have remained in the same positions.

**Note**: You can find the complete documentation for the **fct_relevel()** function from the **forcats** package here.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Convert Factor to Numeric in R

How to Convert Factor to Character in R

How to Reorder Factor Levels in R