Thanks both Uwe and Daniel for the great help!
On Sat, Dec 14, 2013 at 3:30 PM, Dániel Kehl <ke...@ktk.pte.hu> wrote: > Dear Gang, > > this seem to solve your problem. > > > http://stackoverflow.com/questions/1195826/dropping-factor-levels-in-a-subsetted-data-frame-in-r > > > best > daniel > ________________________________________ > Feladó: r-help-boun...@r-project.org [r-help-boun...@r-project.org] ; > meghatalmazó: Gang Chen [gangch...@gmail.com] > Küldve: 2013. december 14. 21:09 > To: r-help > Tárgy: [R] Change factor levels > > Suppose I have a dataframe 'd' defined as > > L3 <- LETTERS[1:3] > d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace > = TRUE)) > (d <- d0[d0$fac %in% c('A', 'B'),]) > > x y fac > 2 1 2 B > 3 1 3 A > 4 1 4 A > 5 1 5 A > 6 1 6 B > 8 1 8 A > > Even though factor 'fac' in 'd' only has 2 levels, but it seems to bear the > birthmark of 3 levels from its parent 'd0': > > str(d) > > 'data.frame': 6 obs. of 3 variables: > $ x : num 1 1 1 1 1 1 > $ y : num 2 3 4 5 6 8 > $ fac: Factor w/ 3 levels "A","B","C": 2 1 1 1 2 1 > > How can I cut the umbilical cord so that factor 'fac' in 'd' would have an > accurate birth certificate with the correct number of levels? Apparently > the following does not work: > > levels(d$fac) <- c('A', 'B') > > Also any reason for this heritage? > > Thanks, > Gang > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.