Skip to content
Prev 131033 / 398503 Next

How to recode a factor level (within the list)?

On Sun, 2 Dec 2007, Lauri Nikkinen wrote:

            
Not so:
x           y           f           g       wdays
   "integer"   "numeric"    "factor"      "Date" "character"

and DF$f does not have any NA levels.

The place you may think you have got NAs is in format(wdays):
they are not NA nor "NA" but "NA     ".  I am not sure what exactly you 
want (NA is not appearing in the tables: see the 'exclude' argument),
but perhaps

lapply(DF, function(x) {
   if(is.character(x)) x[is.na(x)] <- "missing"
   as.data.frame(table(format(x)))
})

or

lapply(DF, function(x) {
   z <- table(format(x))
   names(z)[grep("^NA", names(z))] <- "missing"
   as.data.frame(z)
})

or

lapply(DF, function(x) {
   z <- table(x, exclude=character(0))
   names(z)[is.na(names(z))] <- "missing"
   as.data.frame(z)
})

?