How can I rename factor levels in R, and what are some examples of how to do so?

How can I rename factor levels in R, and what are some examples of how to do so?

Renaming factor levels in R refers to the process of changing the categories or labels associated with the levels of a factor variable. This can be useful for creating more meaningful or descriptive labels, or for standardizing the levels across multiple variables. To rename factor levels in R, the “levels” function can be used to assign a vector of new labels to the existing levels. Alternatively, the “relevel” function can be used to reorder the levels and assign new labels at the same time. Examples of renaming factor levels in R include converting numerical levels to corresponding text labels, changing abbreviations to full names, or combining multiple levels into a single category. By utilizing these functions, users can easily customize their factor levels to better suit their data and analysis needs.

Rename Factor Levels in R (With Examples)


There are two methods you can use to rename factor levels in R:

Method 1: Use levels() from Base R

levels(df$col_name) <- c('new_name1', 'new_name2', 'new_name3')

Method 2: Use recode() from dplyr package

library(dplyr)

data$col_name <- recode(data$col_name, name1 = 'new_name1', 
                                       name2 = 'new_name2',
                                       name3 = 'new_name3')

The following examples show how to use each of these methods in practice.

Method 1: Use levels() Function

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(conf = factor(c('North', 'East', 'South', 'West')),
                 points = c(34, 55, 41, 28))

#view data frame
df

   conf points
1 North     34
2  East     55
3 South     41
4  West     28

#view levels of 'conf' variable
levels(df$conf)

[1] "East"  "North" "South" "West" 

The following code shows how to rename one factor level by name using the levels() function:

#rename just 'North' factor level
levels(df$conf)[levels(df$conf)=='North'] <- 'N'

#view levels of 'conf' variable
levels(df$conf)

[1] "East"  "N"     "South" "West"

And the following code shows how to rename every factor level:

#rename every factor level
levels(df$conf) <- c('N', 'E', 'S', 'W')

#view levels of 'conf' variable
levels(df$conf)

[1] "N" "E" "S" "W"

Example 2: Use recode() Function

The following code shows how to use the recode() function from the dplyr package to rename factor levels:

library(dplyr)

#create data frame
df <- data.frame(conf = factor(c('North', 'East', 'South', 'West')),
                 points = c(34, 55, 41, 28))

#recode factor levels
df$conf <- recode(df$conf, North = 'N',
                           East  = 'E',
                           South = 'S',
                           West  = 'W')

levels(df$conf)

[1] "E" "N" "S" "W"

Note: You can find the complete documentation for the recode() function .

Cite this article

stats writer (2024). How can I rename factor levels in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rename-factor-levels-in-r-and-what-are-some-examples-of-how-to-do-so/

stats writer. "How can I rename factor levels in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-rename-factor-levels-in-r-and-what-are-some-examples-of-how-to-do-so/.

stats writer. "How can I rename factor levels in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rename-factor-levels-in-r-and-what-are-some-examples-of-how-to-do-so/.

stats writer (2024) 'How can I rename factor levels in R, and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rename-factor-levels-in-r-and-what-are-some-examples-of-how-to-do-so/.

[1] stats writer, "How can I rename factor levels in R, and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I rename factor levels in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top