[R] Subset doesn't drop unused factor levels

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Thu Oct 7 20:00:59 CEST 2004



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

No, drop = TRUE is not doing what you're thinking it's doing. From 
?subset (R-1.9.1):

    The 'drop' argument is passed on to the indexing method for data
      frames.

I think you're hoping it is passed on to the indexing method for 
factors, which it isn't.

--sundar




More information about the R-help mailing list