[R] levels of factor when subsetting the factor

Doran, Harold HDoran at air.org
Tue Sep 12 17:36:40 CEST 2006


Also, it is probably easier to use gl() than coerce your data into a
factor

fact <- gl(3, 3, label = c("A", "B", "C")) 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy
> Sent: Tuesday, September 12, 2006 11:32 AM
> To: Afshartous, David; r-help at stat.math.ethz.ch
> Subject: Re: [R] levels of factor when subsetting the factor
> 
> You have at least two choices:
> 
> R> factor(fact[1:6])
> [1] A A A B B B
> Levels: A B
> R> fact[1:6, drop=TRUE]
> [1] A A A B B B
> Levels: A B
> 
> HTH,
> Andy
> 
> 
> From: Afshartous, David
> >  
> > All,
> > 
> > When I take a subset of a factor the reduced factor still maintains 
> > all the original levels of the factor when say forming the key in a 
> > plot.
> > The data is correct, but the variable still "remembers" the 
> original 
> > levels.  See below for reproducible code.  Does anyone know 
> how to fix 
> > this?
> > cheers,
> > dave
> > 
> > fact = as.factor(c(rep("A", 3),rep("B", 3), rep("C", 3))) 
> new.fact = 
> > fact[1:6]
> > > new.fact
> > [1] A A A B B B
> > Levels: A B C    ## should only show A B
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> > 
> > 
> 
> 
> --------------------------------------------------------------
> ----------------
> Notice:  This e-mail message, together with any 
> attachments,...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list