[R] levels of factor when subsetting the factor

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Sep 12 17:32:47 CEST 2006


factor(new.fact) will do the trick. But that will recode the levels and
that might be something you don't want.

> 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
> factor(new.fact)
[1] A A A B B B
Levels: A B

Cheers,

Thierry

------------------------------------------------------------------------
----

ir. Thierry Onkelinx

Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature
and Forest

Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance

Gaverstraat 4

9500 Geraardsbergen

Belgium

tel. + 32 54/436 185

Thierry.Onkelinx op inbo.be

www.inbo.be 


-----Oorspronkelijk bericht-----
Van: r-help-bounces op stat.math.ethz.ch
[mailto:r-help-bounces op stat.math.ethz.ch] Namens Afshartous, David
Verzonden: dinsdag 12 september 2006 17:23
Aan: r-help op stat.math.ethz.ch
Onderwerp: [R] levels of factor when subsetting the factor

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