[R] Creating subsets with factors

Sven Garbade garbade at psy.uni-muenchen.de
Wed Jan 9 13:15:16 CET 2002


Peter Dalgaard BSA wrote:
> 
> Sven Garbade <garbade at psy.uni-muenchen.de> writes:
> 
> > Hi all,
> >
> > I don't understand the following output. I've created a data subset from
> > a data frame by
> >
> > > p1.sub <- subset(p1.dat, vp!="p1")
> >
> > this is ok. But
> >
> > > attach(p1.sub)
> > > vp
> >  [1] p1ab p1ab p1ab p1ab p1ab p1br p1br p1br p1br p1br p1kf p1kf p1kf
> > p1kf p1kf
> > [16] p1mg p1mg p1mg p1mg p1mg p1mw p1mw p1mw p1mw p1mw
> > Levels:  p1 p1ab p1br p1kf p1mg p1mw
> >
> > shows me that the factor vp has 6 levels instead of 5? 5 should be the
> > correct number of levels, because p1 isn't in the data subset.
> 
> Nope. Factors can have levels that are not present in the data set.
> There are good reasons for this. For instance you cannot c(f1,f2) if
> f1 and f2 are factors with different level sets.

Ok, this is evident.

Thanks, Sven
 
> If you want to reduce the levels to those present in the factor, use
> 
> p1.sub$vp <- factor(p1.sub$vp)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list