[R] dropping factor levels in subset
Frank E Harrell Jr
fharrell at virginia.edu
Fri Jun 27 19:27:06 CEST 2003
On Fri, 27 Jun 2003 11:57:02 -0500
Marc Schwartz <mschwartz at medanalytics.com> wrote:
> >-----Original Message-----
> >From: r-help-bounces at stat.math.ethz.ch
> >[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Prof
> >Brian Ripley
> >Sent: Friday, June 27, 2003 1:35 AM
> >To: Marc Schwartz
> >Cc: r-help at stat.math.ethz.ch; 'Nick Bond'
> >Subject: RE: [R] dropping factor levels in subset
> >
> >
> >A more transparent solution is
> >
> >old.factor[1:3, drop = TRUE]
> >
> >That has worked for a long time, but apparently not been
> >documented in R
> >until 1.7.1 (docs added a couple of hours before release). So
> >you could do
> >(probably, since there are some bugs prior to 1.8.0)
> >
> >crb[] <- lapply(crb, function(x) x[drop=TRUE])
> >
> >to remove the unused levels on all factors in the data frame.
>
> SNIP
>
> >
>
> Prof. Ripley,
>
> Thank you for pointing this out. I checked both ?factor and ?"[" and
> note that this behavior is now documented.
>
> A question: How long (roughly) has this been present in R for
> factors?
>
> I ask because I had a vague recollection this morning, after seeing
> your reply, of an exchange between Frank Harrell and others regarding
> just such a 'feature' in R some time ago. It turns out to have been
> back in January of 2002 based upon my search of the r-help archive
> this morning
> (http://maths.newcastle.edu.au/~rking/R/help/01c/3809.html). In this
> exchange, Frank suggested using just such an approach (ie. "x <-
> x[,drop=T]") for factor objects, whereas Peter in that same thread
> noted the use of 'x <- factor(x)' in his reply, which is what I tend
> to use. If my re-read of the thread is correct, I believe that Frank
> was also arguing in favor of a global options() setting regarding this
> behavior.
>
> A recent (May 2003) exchange between Duncan Murdoch and John Chambers
> (http://maths.newcastle.edu.au/~rking/R/devel/03a/1003.html) would
> suggest that such a feature was present for vectors, but perhaps
> incompletely documented as you perhaps suggest, given Duncan's
> question if my read of the exchange is correct.
>
> I now note that for factor objects, this is included in MASS 4 (pg
> 19), whereas it is a footnote in MASS 3 (pg 20) and I could not find
> it in MASS 1 (I don't have a copy of MASS 2 to review). It is also a
> footnote in S Programming (pg 14). Not sure if any significance should
> be attached to being a footnote versus being in the body of the text.
>
> Lastly, I note that references to "[" in the "White Book" include a
> 'drop' argument on pg 465 and in the "Green Book" on pg 340, which
> would suggest that it has been around for some time, at least as a
> high level method, though with no specific reference that I could note
> for factor objects.
>
> Regards and thanks,
>
> Marc
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
I still think that some sort of global option for this is needed. I remain unconvinced that the current default is the most useful one. In my data analysis work I have always wanted to have a subset that was formed on a categorical variable to cause behavior as if the unused levels never existed. Users of Hmisc will find this behavior implemented although I need to check if the subset function works this way with Hmisc.
---
Frank E Harrell Jr Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat
More information about the R-help
mailing list