From: Marc Schwartz (mschwartz@medanalytics.com)
Date: Fri 27 Jun 2003 - 13:35:43 EST
Message-id: <002b01c33c5d$30bfff20$0201a8c0@MARC>
>-----Original Message-----
>From: r-help-bounces@stat.math.ethz.ch
>[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Nick Bond
>Sent: Thursday, June 26, 2003 10:08 PM
>To: r-help@stat.math.ethz.ch
>Subject: [R] dropping factor levels in subset
>
>
>Dear all,
>I've taken a subset of data from a data frame using
>
>crb<-subset(all.raw, creek %in% c("CR") & year %in%
>c(2000,2001) & substrate
>%in% ("b"))
>
>this works fine, except that all of the original factor levels are
>maintained. This results in NA's for these empty levels when I
>try to do
>summaries based on factors using by(). Is there a simple way
>to drop the
>factor levels that are no longer represented. I've used na.omit on
the
>results from by, but then I have to deal with the attr setting, which
>catches me too. Probably a silly question, but I've done a search and
>couldn't find anything. Can someone help me please.
>Regards
>Nick
See ?factor for additional information, but a quick example where
using factor(old.factor) will return the factor with unused levels
dropped.
# Create a factor
> old.factor <- factor(c("One", "Two", "Three", "Four"))
> old.factor
[1] One Two Three Four
Levels: Four One Three Two
# Create a subset of three noting that all four
# levels are retained
> new.factor <- old.factor[1:3]
> new.factor
[1] One Two Three
Levels: Four One Three Two
# Drop unused level
> new.factor2 <- factor(new.factor)
> new.factor2
[1] One Two Three
Levels: One Three Two
HTH,
Marc Schwartz
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:12:09 EST