# droplevels R Example | How to Drop Factor Levels of Vector & Data Frame

Basic R Syntax:

`droplevels(x)`

The droplevels R function removes unused levels of a factor. The function is typically applied to vectors or data frames. The basic code for droplevels in R is shown above.

In the following article, I’ll provide you with two examples for the application of droplevels in R. Let’s dive right in…

## Example 1: Drop Levels of Factor Vector

The simplest way of using droplevels in the R programming language is the application to a factor vector. Let’s create an example vector first:

```x <- factor(c(3, 4, 8, 1, 5, 4, 4, 5))        # Example factor vector
x <- x[- 1]                                   # Delete first entry, i.e. factor level 3
x                                             # Print example vector to RStudio console
# 4 8 1 5 4 4 5
# Levels: 1 3 4 5 8```

Our example vector consists of five factor levels: 1, 3, 4, 5, and 8. However, the vector itself does not include the value 3. The factor level 3 might therefore be dropped. Let’s do this:

```x_drop <- droplevels(x)                       # Apply droplevels in R
x_drop
# 4 8 1 5 4 4 5
# Levels: 1 4 5 8```

As you can see based on the RStudio console output: After applying the R droplevels command, the factor level 3 is removed.

By the way: I have also published a YouTube video, in which I’m explaining the Code of Example 1 in more detail:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

I hope you know at this point how the droplevels function has to be applied to a vector. But what if we want to apply the droplevels function to a data frame? That’s what I’m going to show you next…

## Example 2: Apply droplevels to data.frame in R

In R, it is possible to use the droplevels code for a whole data frame. Let’s create some example data first:

```x1 <- factor(c(7, 3, 7, 7, 5))                 # First column of example data frame
x2 <- factor(c(4, 4, 4, 4, 2))                 # Second column of example data frame
x3 <- c(1, 5, 3, 2, 9)                         # Third column of example data frame

data <- data.frame(x1, x2, x3)                 # Create example data frame
data <- data[1:4, ]                            # Delete bottom row of data frame```

Let’s check the structure of our example data frame by applying the str function:

`str(data)                                      # Check levels of all columns` Graphic 1: Exemplifying Data Frame with Factor Columns.

As you can see, our data consists of three columns: The first column is a factor with 3 levels; the second column is a factor with 2 levels; and the third column is numeric.

Now, let’s apply the droplevels R function to this example data frame…

`data_drop <- droplevels(data)                  # Drop levels of data frame`

…and then let’s examine the factor level structure of the two factor columns:

`str(data_drop)                                 # Check levels after applying droplevels` Graphic 2: Exemplifying Data Frame after the Usage of droplevels in R.

After using the R droplevels command, both factor columns are losing one unused factor level.

Looks good!

## Using Factors in R: Video Examples

Handling factors in R can be tricky. If you need further explanations how to deal with the factor class in R, I can recommend the following video tutorial of the DataCamp YouTube channel:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.