Replace/Rename the Level Value of a Factor Variable in R

Problem:
Replace/Rename the Level Value of a Factor Variable in R

Solution:

Suppose we have a Dataset where we have a Factor Column. Now, the requirement is to change the existing factor level values with some new values. 

For this Example, we need a DataSet with at least one Factor column. We can use the “whiteside” dataset from the “MASS” library. To practice R, we can use the in-built R libraries and Datasets. Details on how to get the in-built Dataset in R is described here


 “whiteside” dataset has a Factor column called “Insul” and the possible values for this column (Levels) are “Before” and “After”. Now, if we want to replace “Before” with “0” and “After” with “1”, we can use the below code.

Code:

Output
1

Leave a Reply

Your email address will not be published. Required fields are marked *