[R] subset drop unused levels

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jun 28 15:20:40 CEST 2004


That's not the purpose, though.  MASS4 p.29 says

  This implies all possible values for
  the index.  It is really only useful on the receiving side, where it
  replaces the contents of the vector but keeps other aspects (the
  class, the length, the names, \dots).

See also R-lang, which says

@strong{Empty}.  The expression @code{x[]} returns @code{x}, but drops
``irrelevant'' attributes from the result.  Only @code{names} and in
multi-dimensional arrays @code{dim} and @code{dimnames} attributes are
retained.

but does not (yet) cover what it calls `Subset assignment'.


On Mon, 28 Jun 2004, Spencer Graves wrote:

>       "an assignment to a square bracket" implies that the object 
> already exists and gives an error if it does not.  Consider the following: 
> 
>  > a[] <- 1:3
> Error: Object "a" not found
>  > a <- 1:2
>  > a[] <- 1:3
> Warning message:
> number of items to replace is not a multiple of replacement length
>  >
> 
>       hope this helps.  spencer graves
> 
> merser at tiscali.dk wrote:
> 
> >thank you
> >sorry, but i missed that thread
> >your solution works (of cause)
> >
> >hopefully the code and not the documentation will be corrected as the drop
> >argument comes very convenient or maybe as a new option:  drop.unused.levels=T
> > 
> >regards soren
> >
> >btw 
> >how do i interpret an assigment to a square bracket?
> >d2[] <- lapply(d2, function(x) if (is.factor(x)) factor(x) else x) 
> >
> >
> >----- Original Message ----- 
> >From: "Peter Dalgaard" <p.dalgaard at biostat.ku.dk>
> >To: "Søren Merser" <merser at image.dk>
> >Cc: "R - help" <r-help at stat.math.ethz.ch>
> >Sent: Sunday, June 27, 2004 11:38 AM
> >Subject: Re: [R] subset drop unused levels
> >
> >
> >  
> >
> >>Søren Merser <merser at image.dk> writes:
> >>
> >>    
> >>
> >>>hi there
> >>>
> >>>tried to use subset with drop=TRUE, but all the 'old' levels are preserved,
> >>>i.e. when calling e.g. ftable a lot of zeros are displayed
> >>>
> >>>      
> >>>
> >>>>x<-subset(LREG,  (kir=='AA' | kir=='BB') & (type=='t1' | otype=='t2'),
> >>>>        
> >>>>
> >>>drop=TRUE, select=c(event, kir, type))
> >>>      
> >>>
> >>>>ftable(x)
> >>>>        
> >>>>
> >>>i explicit have to call factor like
> >>>      
> >>>
> >>>>ftable(event~factor(kir)+factor(type))
> >>>>        
> >>>>
> >>>any hints?
> >>>      
> >>>
> >>Yes, the drop argument is misdocumented. It actually does the same as
> >>the drop argument to [.data.frame. It was on this very list just
> >>eleven days ago, see e.g. the below ref., including what to do if you
> >>really want to get rid of unused levels
> >>
> >>http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg22459.html
> >>
> >>(yet another interface to the archives...)
> >>
> >>-- 
> >>   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
> >>
> >>______________________________________________
> >>R-help at stat.math.ethz.ch mailing list
> >>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >>
> >>    
> >>
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >  
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list