How to use revalue() method In R Language
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 libraries library ( "plyr" ) # 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 revalue (val, c ( "Fun" = "Learning" , "Coding" = "Programming" )) print ( "Modified Levels of factor" ) print ( levels (val)) |
Output:
[1] "Levels of factor" [1] "Coding" "For" "Fun" "Geeks" Geeks For Geeks Programming Learning Levels: [1] "Modified Levels of factor" [1] "Coding" "For" "Fun" "Geeks"
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
Contact Us