I have a data frame with numeric and ordered factor columns. I have lot of NA values, so no level is assigned to them. I changed NA to "No Answer", but levels of the factor columns don't contain that level, so here is how I started, but I don't know how to finish it in an elegant way:
addNoAnswer = function(df) {
factorOrNot = sapply(df, is.factor)
levelsList = lapply(df[, factorOrNot], levels)
levelsList = lapply(levelsList, function(x) c(x, "No Answer"))
...
Is there a way to directly apply new levels to factor columns, for example, something like this:
df[, factorOrNot] = lapply(df[, factorOrNot], factor, levelsList)
Of course, this doesn't work correctly.
I want the order of levels preserved and "No Answer" level added to last place.
See Question&Answers more detail:os