droplevels Function in R (Example)

The R code in this tutorial illustrates how to drop unused levels of a factor with the droplevels() function in the R programming language.

Example Data

my_factor <- factor(LETTERS[1:5])     # Create example factor
my_factor <- my_factor[-1]
my_factor                             # Print factor to RStudio console
# [1] B C D E
# Levels: A B C D E

Example: Drop Factor Levels with droplevels Function

my_factor <- droplevels(my_factor)
my_factor                             # Print updated factor
# [1] B C D E
# Levels: B C D E

As you can see based on the previously shown output of the RStudio console, our updated factor does not contain the factor level A anymore.

Video Examples

The following video of the Statistics Globe YouTube channel shows further examples for the application of the droplevels function in R:

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top