Replace contents of factor column in R dataframe
In this article, we are going to see how to replace the content of the factor column in the dataframe in R Programming Language.
Example 1: Replacing content of factor column
Initially, the factor column is converted to a character column using the explicit conversion of as.character() method in R. This converts the specified column to character type, which can contain duplicate element values, without all copies belonging to the same level.
This is followed by the replacement of the old cell value of this column with a new one. The column is then revered back again to factor type using as.factor() method.
The levels of the data frame column can be printed using the levels() method. If we display the levels before and after replacement, they will differ.
Example 2: Replacing level of the factor column
The levels of the data frame can also be modified using the levels() method. The old level value can be compared using the data frame indexing method and then assigned a new value. All the occurrences of this level are changed to the new assigned level. The number of levels, in this case, remain same in number but change in value.