I need to replace the levels of a factor column in a dataframe. Using the iris dataset as an example, how would I replace any cells which contain virginica with setosa in the Species column?
I expected the following to work, but it generates a warning message and simply inserts NAs:
iris$Species[iris$Species == 'virginica'] <- 'setosa'
I bet the problem is when you are trying to replace values with a new one, one that is not currently part of the existing factor’s levels:
Your example was bad, this works:
This is what more likely creates the problem you were seeing with your own data:
It will work if you first increase your factor levels:
If you want to replace "species A" with "species B" you’d be better off with