droplevels() function in R programming used to remove unused levels from a Factor.
Syntax:
# For vector object
droplevels(x, exclude = if(anyNA(levels(x))) NULL else NA, …)# For data frame object
droplevels(x, except, exclude)Parameter values:
x represents object from which unused level has to be dropped
exclude represents factor levels which should be excluded even if present
except represents indices of columns from which levels should not be dropped
Example 1:
# Defining vector x <- c(1, 3, 4, 8, 1, 5, 4, 4, 5, 6) # Defining factor object for vector f <- factor(x) # Print factor object cat("Factor object before deleting value:\n") print(f) cat("\n") # Delete value at index 2 f <- f[-2] # Print factor object cat("Factor object after deleting value:\n") print(f) cat("\n") cat("After dropping unused level:\n") new_f <- droplevels(f) print(new_f) |
Output:
Factor object before deleting value: [1] 1 3 4 8 1 5 4 4 5 6 Levels: 1 3 4 5 6 8 Factor object after deleting value: [1] 1 4 8 1 5 4 4 5 6 Levels: 1 3 4 5 6 8 After dropping unused level: [1] 1 4 8 1 5 4 4 5 6 Levels: 1 4 5 6 8
Example 2:
# Defining columns x <- factor(c(7, 3, 3, 7, 5, 5, 1)) y <- factor(c(1, 1, 1, 4, 4, 4, 2)) z <- c(1, 5, 3, 2, 9, 4, 7) # Defining data frame df <- data.frame(x, y, z) df <- df[1:6, ] # Print structure of data frame str(df) cat("\n") # Drop levels from data frame df_drop <- droplevels(df) # Print structure of new data frame str(df_drop) |
Output:
'data.frame': 6 obs. of 3 variables: $ x: Factor w/ 4 levels "1", "3", "5", "7": 4 2 2 4 3 3 $ y: Factor w/ 3 levels "1", "2", "4": 1 1 1 3 3 3 $ z: num 1 5 3 2 9 4 'data.frame': 6 obs. of 3 variables: $ x: Factor w/ 3 levels "3", "5", "7": 3 1 1 3 2 2 $ y: Factor w/ 2 levels "1", "4": 1 1 1 2 2 2 $ z: num 1 5 3 2 9 4
Recommended Posts:
- Get or Set Levels of a Factor in R Programming - levels() Function
- Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function
- Get the Number of Levels of a Factor in R Programming - nlevels() Function
- Checking if the Object is a Factor in R Programming - is.factor() Function
- Convert a Vector into Factor in R Programming - as.factor() Function
- Convert an Unordered Factor to an Ordered Factor in R Programming - as.ordered() Function
- Check if a Factor is an Ordered Factor in R Programming - is.ordered() Function
- Convert Factor to Numeric and Numeric to Factor in R Programming
- Generate Factors with specified Levels in R Programming - gl() Function
- Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
- Replace values of a Factor in R Programming - recode_factor() Function
- Performing Analysis of a Factor in R Programming - factanal() Function
- The Factor Issue in a DataFrame in R Programming
- Check if a Function is a Primitive Function in R Programming - is.primitive() Function
- Compute Density of the Distribution Function in R Programming - dunif() Function
- Apply a Function over a List of elements in R Programming - lapply() Function
- Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
- Compute the Natural Logarithm of the Absolute Value of Gamma Function in R Programming - lgamma() Function
- Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function
- Compute the Second Derivative of the Logarithmic value of the gamma Function in R Programming - trigamma() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

