How to use the base R method In R Language

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 factor
val < - factor(c("Geeks", "For", "Geeks",
                 "Coding", "Fun"))
  
# printing the levels of factor variables
print("Levels of factor")
lvls < - levels(val)
print(lvls)
  
# renaming the factors
levels(val)[levels(val) == "Coding"] < - "Learning"
print("Modified Levels of factor")
lvls < - levels(val)
print(lvls)


Output

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks" 
[1] "Modified Levels of factor"
[1] "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 variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
levels(val) <- list("Learning"="Fun",
                    "Programming"="Coding")
print("Modified Levels of factor")
print(levels(val))


Output:

[1] "Levels of factor"
[1] "Coding" "For"    "Fun"    "Geeks" 
[1] "Modified Levels of factor"
[1] "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 variables
print("Levels of factor")
lvls <- levels(val)
print(lvls)
  
# renaming the factors
levels(val) <- c("Hi","There",
                 "Welcome","Here")
print("Modified Levels of factor")
print(levels(val))


Output:

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

How to Rename Factor Levels in R?

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 

Similar Reads

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 :...

Method 2: Using revalue() method

...

Method 3: Using mapvalues() method

...