[R] Including only a subset of the levels of a factor XXXX

peter dalgaard pdalgd at gmail.com
Fri Sep 2 20:19:24 CEST 2011

On Sep 1, 2011, at 21:11 , R. Michael Weylandt wrote:

> Dropping all occurences of a factor does not drop that level. This actually
> turns out to be much more useful than it first might appear, but if you
> really need to get around it, it can be done.

...most expediently by using factor(), as others have pointed out. Or droplevels() for data frames.

We had the converse issue just the other day (Aug 30) when someone had problems with "showing zero frequencies in xtabs", which turned out to be caused by the tabulated data _not_ being factors, hence not containing information about which values could have been there but wasn't.

The behavior of subsetting operators is so as to make things like tables and barplots consistent across subsets, but there are cases where you want the extra levels dropped. However, the default is as it is, because it is easier to drop levels than to reinstate them. Neither is impossible, of course.

Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
"Døden skal tape!" --- Nordahl Grieg

More information about the R-help mailing list