s-news
[Top] [All Lists]

Re: factor levels

To: s-news@lists.biostat.wustl.edu
Subject: Re: factor levels
From: Gregor Gorjanc <gregor.gorjanc@bfro.uni-lj.si>
Date: Fri, 19 Jan 2007 11:55:52 +0000 (UTC)
References: <20070119003842.30907.qmail@web57105.mail.re3.yahoo.com>
User-agent: Loom/3.14 (http://gmane.org/)
yiwu ye <yiwu21111958 <at> yahoo.com> writes:
> Dear list,I have the following problem. Could anyone
help?x_as.factor(letters[1:5])x_x[-4]levels(x)The levels(x) still gives you 5
rather than 4 levels. Of course, I can do levels(as.factor(as.character(x)) to 
get rid of the non-existing factor level. But, I think there must be a simpler
way.ThanksYiwu

Not sure about S-plus, but in R you can simply do

myFac <- factor(myFac)

and unused levels will be dropped. Package gdata has drop.levels function, 
which 
also works on data.frames. I use it in my work flow like this:

1. import data
2. massage it if neccesarry
3. use drop.levels at the end and it will remove all unused levels in a 
   data.frame

Gregor




<Prev in Thread] Current Thread [Next in Thread>