Thursday, October 7, 2010

how to drop unused levels in R

problem.factor <- problem.factor[, drop = TRUE]

To drop unused levels from all factors in a data.frame, use this function:

drop.levels <- function(dat){
  # Drop unused factor levels from all factors in a data.frame
  # Author: Kevin Wright.  Idea by Brian Ripley.
  dat[] <- lapply(dat, function(x) x[,drop=TRUE])


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner