How to Rename Factor Levels in R?

• Last Updated : 28 Nov, 2021

In this article, we are going to how to rename factor levels in R programming language.

A factor variable in R is represented using categorical variables which are represented using various levels. Each unique value is represented using a unique level value. A factor variable or vector in R can be declared using the factor() method.

Syntax: factor(vec)

Arguments : vec – The vector

The unique levels of the factor can be extracted using the levels() method which takes as argument the factor vector created. It displays all the unique entries within the factor variable.

Syntax: levels(fac-vec)

Arguments : fac-vec – The factor vector to extract the values from

Method 1: Using the base R method

The factor levels can be renamed using the comparison and indexing operators. The existing factor value is compared and then modified by assigning it to a new value. The changes are made to the existing factor vector. The following syntax is followed :

Syntax: levels(fac-vec)[levels(fac-vec)==old-val] <- new-val

Arguments: fac-vec – The factor vector to extract the values from

R

 # declare a factorval < - factor(c("Geeks", "For", "Geeks",                 "Coding", "Fun"))  # printing the levels of factor variablesprint("Levels of factor")lvls < - levels(val)print(lvls)  # renaming the factorslevels(val)[levels(val) == "Coding"] < - "Learning"print("Modified Levels of factor")lvls < - levels(val)print(lvls)

Output

 "Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"
 "Modified Levels of factor"
 "Learning" "For"      "Fun"      "Geeks"

The list() method can also be used to create a list of factor levels to be created and then can be reassigned to new values. The levels() are then reassigned to these newly declared values. The changes are made to the original factor vector. More than one factor can be renamed using this method.

list(new-fac=val = old-fac-val,..)

A combination of old and new factor variable values are declared inside the list() method.

R

 # declare a factor val <- factor(c("Geeks","For","Geeks",                "Coding","Fun"))  # printing the levels of factor variablesprint("Levels of factor")lvls <- levels(val)print(lvls)  # renaming the factorslevels(val) <- list("Learning"="Fun",                    "Programming"="Coding")print("Modified Levels of factor")print(levels(val))

Output:

 "Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"
 "Modified Levels of factor"
 "Learning"    "Programming"

All levels of a factor variable can also be renamed using the c() method to create a vector of the levels. The newly created values are then assigned to a factor variable. The following code snippet illustrates this procedure :

R

 # declare a factor val <- factor(c("Geeks","For","Geeks",                "Coding","Fun"))  # printing the levels of factor variablesprint("Levels of factor")lvls <- levels(val)print(lvls)  # renaming the factorslevels(val) <- c("Hi","There",                 "Welcome","Here")print("Modified Levels of factor")print(levels(val))

Output:

 "Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"
 "Modified Levels of factor"
 "Hi"      "There"   "Welcome" "Here"

Method 2: Using revalue() method

The plyr package in R is used to split data apart, perform operations upon it and mash it back together. The package can be downloaded and installed into the working space using the following command :

install.packages("plyr")

The revalue method in this package is used to replace the specified values with newly defined values, in a factor vector. The levels of the factor variable are not modified in this case and remain original.

Syntax: revalue(x, replace = NULL)

Arguments :

• x – factor vector
• replace – factor vector, with new values as values, and old values as names.

R

 # importing req librarieslibrary("plyr")  # declare a factor val <- factor(c("Geeks","For","Geeks",                "Coding","Fun"))  # printing the levels of factor variablesprint("Levels of factor")lvls <- levels(val)print(lvls)  # renaming the factorsrevalue(val, c("Fun"= "Learning", "Coding"= "Programming"))print("Modified Levels of factor")print(levels(val))

Output:

 "Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"
Geeks For Geeks Programming Learning
Levels:
 "Modified Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"

Method 3: Using mapvalues() method

The mapvalues() method in the plyr package in R is used to replace the specified values with new values, in the factor vector. The changes are not retained in the original vector.

Syntax: mapvalues(x, from, to)

Arguments :

• x – the factor vector to modify
• from – a vector of the items to replace
• to – a vector of replacement values

R

 # importing req librarieslibrary("plyr")  # declare a factor val <- factor(c("Geeks","For","Geeks",                "Coding","Fun"))  # printing the levels of factor variablesprint("Levels of factor")lvls <- levels(val)print(lvls)  # renaming the factorsmapvalues(val, from = c("Fun","Coding"),          to = c("Learning", "Programming"))print("Modified Levels of factor")print(levels(val))

Output:

 "Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"
Geeks For Geeks Programming Learning
Levels:
 "Modified Levels of factor"
 "Coding" "For"    "Fun"    "Geeks"

My Personal Notes arrow_drop_up