[R] subset(..., drop=TRUE) doesn't seem to work.

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jun 16 17:35:51 CEST 2004


Paul Lemmens <P.Lemmens at nici.kun.nl> writes:

> >> Could you comment?
> >
> > Looks like a documentation bug. The actual code ends up  doing
> >
> >     x[r, vars, drop = drop]
> >
> > and "[.data.frame" will not drop factor levels. I wonder if it ever
> > did...
> >
> Bottomline: unless I find the time to submit a patch for
> '[.data.frame', I'll need to use the more elaborate way of dropping
> the unused levels?
> 
> Does "will not drop" imply that it cannot be programmed, should not be
> programmed, or has not been programmed yet?

Cannot. The drop argument is doing something different. 

Anyways, the way out is

 d2 <- subset(dd,c==1)
 ifac <- sapply(dd,is.factor)
 d2[ifac] <- lapply(d2[ifac],factor)

or 

 d2 <- subset(dd,c==1)
 d2[] <- lapply(d2, function(x) if (is.factor(x)) factor(x) else x) 



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