Thursday, March 10, 2011

R: Drop factor levels in a dataset

R has factors, which are very cool (and somewhat analogous to labeled levels in Stata). Unfortunately, the factor list sticks around even if you remove some data such that no examples of a particular level still exist

# Create some fake data
x <- as.factor(sample(head(colors()),100,replace=TRUE))
levels(x)
x <- x[x!="aliceblue"]
levels(x) # still the same levels
table(x) # even though one level has 0 entries!

The solution is simple: run factor() again:
x <- factor(x)
levels(x)

If you need to do this on many factors at once (as is the case with a data.frame containing several columns of factors), use drop.levels() from the gdata package:
x <- x[x!="antiquewhite1"]
df <- data.frame(a=x,b=x,c=x)
df <- drop.levels(df)

Now I'm going to quit monkeying around and get to sleep.

3 comments:

  1. Thank you very much! Finding this after looking 3 hours for a solution.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. OK, so I get that. How do you solve the reverse problem? Where R has assigned levels to a string of numerics, thereby preventing you from say... plotting them as numbers? I had a string of numbers, I cbind() them to some characters and "poof" the numbers are now a factor. Yuch. I can't find any function that removes a factor from a column in a dataframe.

    Can you point me to a way to remove an unwanted factor, while keeping the underlying numbers?

    ReplyDelete