rmodel.matrix

How to remove unused levels from model matrix


I created a model matrix. Some variables are categorial variables. After filtering the data some levels are not in the data set anymore. How can i remove the unused levels? Can i apply the factor function on the categorial variables ?


Solution

  • you can use the droplevels function in base R. Let x be your factor/ categorical variable:

    x <- as.factor(c("cat", "dog","cat", "gator"))
    x
    # [1] cat   dog   cat   gator
    # Levels: cat dog gator
    
    # somewhere in analysis you removed the only entry for a level
    x <- x[x!= "gator"]     
    x
    # [1] cat dog cat
    # Levels: cat dog gator
    
    droplevels(x)
    # [1] cat dog cat
    # Levels: cat dog
    

    Refer droplevels R documentation for more details.