[R] Creating subsets with factors
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Wed Jan 9 11:07:52 CET 2002
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.
If you want to reduce the levels to those present in the factor, use
p1.sub$vp <- factor(p1.sub$vp)
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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