2

Possible Duplicate:
dropping factor levels in a subsetted data frame in R

I have a data frame which has a factor column, then I would like to use subset to extract only part of its data. But the extracted data frame's factor column still has the same levels even some levels has no value. This would impact my following actions (like visualization using ggplot).

The following is a sample code.

d<-data.frame(c1=factor(c(1,1,2,3)),c2=c("a","b","c","d"))
d<-subset(d,c1 %in% c(1,2))
d$c1

The column c1 still have 3 levels (1,2,3), but actually I'd like to it to be (1,2), because these's no value for level 3. Then in visualization, I won't draw any graph for level 3.

How can I achieve that ? Thanks

Community
  • 1
  • 1
zjffdu
  • 25,496
  • 45
  • 109
  • 159

1 Answers1

3

Use droplevels:

d <- droplevels(d)

Marius
  • 58,213
  • 16
  • 107
  • 105