Sunday, April 7, 2013

R - drop unused levels from factors

  • drops unused levels from factors
  • can be used on a factor or a data frame (which makes just sense when the data frame contains any factor)
  • syntax: droplevels(df)
  • create an example data frame
exdf <- data.frame(x=factor(1:10,labels=letters[1:10]),y=rbinom(10,1,0.5))
exdf
x y
1  a 0
2  b 1
3  c 1
4  d 0
5  e 1
6  f 1
7  g 0
8  h 0
9  i 1
10 j 1
  • subset exdf by y==1
exdf <- exdf[exdf$y==1,]
summary(exdf$x)
a b c d e f g h i j 
0 1 1 0 1 1 0 0 1 1
  • we see that all levels of y are present in the summary although several are not used anymore
exdf <- droplevels(exdf)
summary(exdf$x)
b c e f i j 
1 1 1 1 1 1

No comments :

Post a Comment