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 virginic
You want to replace the values in a dataset column, but you're getting an error like this:
invalid factor level, NA generated
Try this instead:
levels(dataframe$column)[levels(dataframe$column)=='old_value'] <- 'new_value'
A more general solution that works with all the data frame at once and where you don't have to add new factors levels is:
data.mtx <- as.matrix(data.df)
data.mtx[which(data.mtx == "old.value.to.replace")] <- "new.value"
data.df <- as.data.frame(data.mtx)
A nice feature of this code is that you can assign as many values as you have in your original data frame at once, not only one "new.value"
, and the new values can be random values. Thus you can create a complete new random data frame with the same size as the original.