Monday, August 22, 2011

refactor in R

I use this little R function almost daily.  It just takes a factor, drops the unused levels, and otherwise keeps the same ordering.
refactor <- function(x) {
  x <- factor(x, levels=levels(x)[levels(x) %in% x] )
  return(x)
}

File this under "commands that should be included in R."

No comments:

Post a Comment