[R] levels of factor

Kevin Bartz kbartz at loyaltymatrix.com
Tue Aug 17 18:46:00 CEST 2004

Believe it or not, that's a feature, not a bug. The idea is that the factor
COULD take on those levels, even if it doesn't in your particular subset. To
drop them, you would have to re-initialize the factor as such:

a$column2 <- factor(a$column2)

Or, you could just download the Hmisc package, which redefines the subset
operator "[" to behave as you'd like. Personally, I think the default
behavior is clearer, however.

By the way, there are some problems with your code. First of all, you should
drop the quotes around column2--they're unnecessary. Secondly, your subset
is redundant: only one of your factor levels can be numbered 1, so only one
of the levels "factor1" and "factor2" is getting included in the result
(whichever is numbered 1 -- I'm guessing it's "factor1"). Was this your


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Luis Rideau Cruz
Sent: Tuesday, August 17, 2004 7:30 AM
To: r-help at stat.math.ethz.ch
Subject: [R] levels of factor


I have a data frame wich I subset like :

a <- subset(df,df$"column2" %in% c("factor1","factor2")  & df$"column2"==1)

But when I type levels(a$"column2") I still get the same levels as in df (my
original data frame)

Why is that?
Is it right?


Luis Ridao Cruz
Nóatún 1
P.O. Box 3051
FR-110 Tórshavn
Faroe Islands
Phone:             +298 353900
Phone(direct): +298 353912
Mobile:             +298 580800
Fax:                 +298 353901
E-mail:              luisr at frs.fo
Web:                www.frs.fo

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list