Search code examples
rr-factor

How do I drop a factor level with no observations?


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

I have a data frame with several variables that I'm running a mixed model on using lme(). One of the variables, ForAgeCat, has five factor levels: 1,2,3,4,5.

str(mvthab.3hr.fc$ForAgeCat)
 >Factor w/ 5 levels "1","2","3","4",..: 5 5 5 5 5 5 5 5 5 5 ...

The problem is that factor level 3 actually doesn't exist, that is, in this dataset (which is a subset of a larger dataset) there are no observations from factor level 3, which I think is messing with my modeling in lme(). Can someone help me to remove/eliminate factor level 3 from the list of factor levels?


Solution

  • use the function droplevels, like so:

    > DF$factor_var = droplevels(DF$factor_var)
    

    More detail:

    > # create a sample dataframe:
    > col1 = runif(10)
    > col1
        [1] 0.6971600 0.1649196 0.5451907 0.9660817 0.8207766 0.9527764 
            0.9643410 0.2179709 0.9302741 0.4195046
    > col2 = gl(n=2, k=5, labels=c("M", "F"))
    > col2
        [1] M M M M M F F F F F
        Levels: M F
    > DF = data.frame(Col1=col1, Col2=col2)
    > DF
         Col1 Col2
     1  0.697    M
     2  0.165    M
     3  0.545    M
     4  0.966    M
     5  0.821    M
     6  0.953    F
     7  0.964    F
     8  0.218    F
     9  0.930    F
     10 0.420    F
    
    > # now filter DF so that only *one* factor value remains
    > DF1 = DF[DF$Col2=="M",]
    > DF1
       Col1 Col2
    1 0.697    M
    2 0.165    M
    3 0.545    M
    4 0.966    M
    5 0.821    M
    
    > str(DF1)
        'data.frame':   5 obs. of  2 variables:
       $ Col1: num  0.697 0.165 0.545 0.966 0.821
       $ Col2: Factor w/ 2 levels "M","F": 1 1 1 1 1
    
    > # but still 2 factor *levels*, even though only one value
    
    > DF1$Col2 = droplevels(DF1$Col2)
    > # now Col2 has only a single level:
    > str(DF1)
       'data.frame':    5 obs. of  2 variables:
      $ Col1: num  0.697 0.165 0.545 0.966 0.821
      $ Col2: Factor w/ 1 level "M": 1 1 1 1 1