I have a list of factors and each factor may has some NA
. Now I want to add a level for NA
and rename it as Missing
. I also what to make sure that Missing
is the last one in each level. Here is my code but it doesn't work.
data = list(a = factor(c(1,1,2,2,3,NA,NA)),
b = factor(c("a","b","b")),
c = factor(c(3,4,NA,3)))
data = lapply(data, FUN = function(x) {
if (any(is.na(x))) {
x = addNA(x)
levels(x)[length(levels(x))] = "Missing"
}
})
Any help would be appreciated.