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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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. Thanks, Sven -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
[snipped...]> I have not needed to use c(f1, f2) but it seems to me that > Peter's example points out more a deficiency in c or the need > for another binding function for this case (which can be done > with factor(c(as.character(f1),as.character(f2))) depending > on how NAs are handled.More often than I'd like, I have to combine several related data frames together. The following showed that the rbind method for data frames does do a reasonable job: # Suppose dat1 and dat2 are two data frames with same variables.> dat1$ind <- factor(rep("dat1", nrow(dat1))) > dat2$ind <- factor(rep("dat2", nrow(dat2))) > dat <- rbind(dat1, dat2) > levels(dat$ind)[1] "dat1" "dat2"> Frank Harrell[more snipped...] Andy -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._