[R] Change factor levels

Dániel Kehl kehld at ktk.pte.hu
Sat Dec 14 21:30:54 CET 2013


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-bounces at r-project.org [r-help-bounces at r-project.org] ; meghatalmazó: Gang Chen [gangchen6 at 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 at 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.



More information about the R-help mailing list