[R] subset problem (reducing from six to two levels)

Philipp Pagel p.pagel at wzw.tum.de
Thu Feb 5 22:49:09 CET 2009

On Thu, Feb 05, 2009 at 01:01:59PM -0800, Ine wrote:
> Hi all,
> I have got a seemingly simple problem (I am an R starter) with subsetting my
> data set, but cannot figure out the solution: I want to subset a data set
> from six to two levels, so that all analyses are done only with these two
> remaining levels.
> I tried
> TOTAL<-read.delim('total.csv',header=T)
> SUBSET.OF.TOTAL<-subset(TOTAL, FactorX %in% c("Level1","Level2"))
> but R does not eliminate the remaining levels of FactorX,

One solution is to have the factors re-built after subsetting:

foo = factor(c('a','a','b','c','c'))

# unused levels persistent:
> foo[foo=='a']
[1] a a
Levels: a b c

# but:
> factor(foo[foo=='a'])
[1] a a
Levels: a


Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany

More information about the R-help mailing list