Search code examples
rsubsetr-factor

Ghost factor levels in R


Possible Duplicate:
dropping factor levels in a subsetted data frame in R

I have subsetted away observations with a certain factor level. When checking whether this has been done with summary() the levels were still listed, but with zero observations. Shouldn't they disappear during the subsetting?


Solution

  • Subsetting doesn't drop empty levels. Why this is the case is that it is a feature. Think of it as your factor levels determine the possible/potential categories of a thing. If you only take a subset of these things, the possible categories of thing don't change, your subset just doesn't contain any of them.

    If you want to drop these empty levels, see ?droplevels.