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

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
19 Jun 2002 19:16:17 +0200


rjvbertin@hotmail.com writes:

> 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. 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.

Not a bug (in any sense). Basically, you do need factors with levels
that are not present once in a while (we had a tabulation example
recently), and it is easier to remove unwanted levels than it is to
get the levels back in. E.g.

> x[-1]
[1] medium large 
Levels:  large medium small 

> x[-1,drop=T]
[1] medium large 
Levels:  large medium 

> factor(x[-1])
[1] medium large 
Levels:  large medium 

As for the usefulness, consider this

> with(subset(juul,sex==1), table(menarche))
numeric(0)
> juul$menarche <- factor(juul$menarche)
> with(subset(juul,sex==1), table(menarche))
menarche
1 2 
0 0 


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._