[R] Subset doesn't drop unused factor levels

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Oct 7 20:05:31 CEST 2004


hadley wickham <h.wickham at gmail.com> writes:

> a <- data.frame(b = rep(1:5, each=2), c=factor(rep("a",10), levels=c("a","b")))
> levels(subset(a, b=1, drop=T)$c)
> # [1] "a" "b"
> 
> Is this a bug?

In some older versions of R (at least older than 1.9.0), there was a
documentation bug in that the help page said that it would drop unused
levels. Nowadays, it says 

    drop: passed on to '[' indexing operator.

which is correct and the docs for [.data.frame

    drop: logical.  If 'TRUE' the result is coerced to the lowest
          possible dimension: however, see the Warning below.

(and that Warning seems to have a typo, but leave that for now...). So
no, it is not a bug, that's not what that argument is for.

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




More information about the R-help mailing list