[R] Subset doesn't drop unused factor levels

Liaw, Andy andy_liaw at merck.com
Thu Oct 7 20:10:41 CEST 2004


> From: hadley wickham
> 
> 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?

Don't think so:

> args("[.data.frame")
function (x, i, j, drop = if (missing(i)) TRUE else length(cols) == 
    1) 
NULL

So the `drop' argument is passed to the "[" method for data.frame (as
documented in ?subset), and not the "[" method for factor, as that's never
called.

Andy
 
> Thanks,,
> 
> Hadley
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list