Consider a data frame containing a factor.

When I create a subset using subset() or any other indexing function, then a new data frame is created.

I have observed that the factor variable retains all of its original levels, even if they do not exist in the new data frame.

This creates problems while plotting or using the functions that rely on factor levels.

Is there any way to remove levels from a factor in the new data frame i.e. the data frame I have taken a subset of

Below is my example:

```data <- data.frame(letters=letters[1:10],
numbers=seq(1:10))

levels(data\$letters)
##  "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

subdata <- subset(data, numbers <= 5)
##   letters numbers
## 1       a       1
## 2       b       2
## 3       c       3
## 4       d       4
## 5       e       5
## But the  levels are still there!
levels(subdata\$letters)
##  "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"```
Apr 17, 2018 4,580 views

## 1 answer to this question.

You can use factor(ff) to drop levels that do not occur

```factor(ff)
# drops the levels that do not occur```

For dropping levels from all factor columns in a data frame, you can use:

```subdata <- subset(data, numbers <= 5)

##   letters numbers
## 1       a       1
## 2       b       2
## 3       c       3
## 4       d       4
## 5       e       5 ```
`subdata[] <- lapply(subdata, function(x) if(is.factor(x)) factor(x) else x)`

NOTE: You can use the above command in a single dataframe with minimal columns. But, if you have large number of columns then you have to use drop.levels() from gdata.

• 2,090 points

edited Apr 17, 2018

+1 vote

## How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

## How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

+1 vote

## How can I drop columns by name in a data frame ?

We can Drop Columns by name in ...READ MORE

## Drop unused levels from a data frame in R

You can use this command droplevels() y <- ...READ MORE

+1 vote

## How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

## In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE