Skip to content
Prev 112552 / 398500 Next

substitute NA values

On Fri, 2007-03-30 at 16:25 +0200, Sergio Della Franca wrote:
This is R, there is always a way (paraphrasing an R-Helper the name of
whom I forget just now). If you mean a canned function, not that I'm
aware of.

Here is one way:

## some example data - not exactly like yours
set.seed(1234)
dat <- data.frame(test = sample(c("t","f"), 9, replace = TRUE), 
                  num = c(10,14,25,NA,40,45,44,47,NA))

## add an NA to dat$test to match your example
dat$test[8] <- NA

## print out dat
dat

## count the various options in $test and return the name of
## the most frequent
freq <- names(which.max(table(dat$test)))

## replace NA in $test with most frequent
dat$test[is.na(dat$test)] <- freq

## print out dat again to show this worked
dat

There may be better ways - the names(which.max(table(...))) seems a bit
clunky to me but it is Friday afternoon and it's been a long week...

And, as this /is/ R, you could wrap that into a function for you use on
other data sets, but I'll leave that bit up to you.

HTH

G