Skip to content
Prev 13851 / 398502 Next

Making a factor with common levels ...

The combine.levels function in the Hmisc library is
related to this:

combine.levels <- function(x, minlev=.05) {
  x <- as.factor(x)
  lev <- levels(x)
  f <- table(x)/sum(!is.na(x))
  i <- f < minlev
  si <- sum(i)
  if(si==0) return(x)
  levels(x) <- if(si==1) list(names(sort(f))[1:2]) else
    list(OTHER=names(f)[i])
  x
}

This combines levels that have a relative frequency
below 'minlev' into new categories.  -Frank Harrell
j.logsdon at lancaster.ac.uk wrote: