# 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 |

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 |

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: