levels() counter-intuitif? (PR#1693)

ripley@stats.ox.ac.uk ripley@stats.ox.ac.uk
Wed, 19 Jun 2002 18:18:18 +0100 (BST)

On Wed, 19 Jun 2002 rjvbertin@hotmail.com wrote:

> Suppose I have a factor size with levels "small", "medium" and "large".
> Then, when I subset this factor:
> >ss<-size[size!="medium"]
> to get at the extremes,
> >levels(ss)
> ....
> Levels:  large medium small
> The same happens with
> >subset( size, size!="medium")
> I understand that the resulting factor inherits the possible levels from its
> "parent", but I do not see in what that is useful.

When you have enough experience you may.  Lots of R code relies on it, for
a start.  It's also essential when predicting a classifcation to know wha
the possible values were.  That's the whole point of a factor: it's an
enumeration type, and one does not change the enumeration on subsetting.

>  Intuitively, I would
> expect that levels(ss) return "small" and "large". For subset(), there might
> be an option to request inheritance of the number of levels.

There is such an option:  [, drop=TRUE].

Please DO NOT use R-bugs to document your intuition.

Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch