Add extra level to factors in dataframe

enedene picture enedene · Apr 26, 2014 · Viewed 65.8k times · Source

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.

Answer

ilir picture ilir · Apr 26, 2014

You could define a function that adds the levels to a factor, but just returns anything else:

addNoAnswer <- function(x){
  if(is.factor(x)) return(factor(x, levels=c(levels(x), "No Answer")))
  return(x)
}

Then you just lapply this function to your columns

df <- as.data.frame(lapply(df, addNoAnswer))

That should return what you want.