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

**Basic R Syntax:**

droplevels(x) |

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 |

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 |

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.

Perfect! 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 |

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 |

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 |

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 |

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:

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R