Table of Contents
In R, factor levels can be renamed using the “levels” function. This function takes two arguments – the first argument is the vector of the current factor levels, and the second argument is the vector containing the new labels. The function then assigns the new labels to the corresponding factor levels. This is a useful way to make data more meaningful and easier to interpret.
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 .