Removing Levels from a Factor in R Programming - droplevels() Function Last Updated : 05 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Python3 # 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: Python3 # 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 Comment More infoAdvertise with us Next Article Get or Set Levels of a Factor in R Programming - levels() Function U utkarsh_kumar Follow Improve Article Tags : R Language R Factor-Function Similar Reads Convert a Vector into Factor in R Programming - as.factor() Function as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters:Â Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in RR # Creating a vector x<-c("female", "male", "ma 1 min read Get or Set Levels of a Factor in R Programming - levels() Function levels() function in R Language is used to get or set the levels of a factor. Syntax: levels(x) Parameters: x: Factor Object Example 1: Python3 1== # R program to get levels of a factor # Creating a factor gender <- factor(c("female", "male", "male", "female 1 min read Remove names or dimnames from an Object in R Programming - unname() Function unname() function in R Language is used to remove the names or dimnames from an Object. Syntax: unname(x) Parameters: x: Object Example 1: Python3 1== # R Program to remove names from an object # Creating a matrix A = matrix(c(1:9), 3, 3) # Naming rows rownames(A) = c("a", "b", 1 min read Replace values of a Factor in R Programming - recode_factor() Function Factors in R programming are kind of data structures that stores categorical data i.e., levels and can have any type of data (integer, string, etc). recode_factor() function in R Language is used to replace certain values in a factor. To use recode_factor() function, dplyr package is required. Synta 1 min read Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function by() function in R programming is an object-oriented wrapper function which performs the provided function on factor levels of the data set passed in the arguments of function call. Syntax: by(data, INDICES, FUN) Parameters: data: represents the dataset INDICES: represents the factor list of dataset 1 min read Create Subsets of a Data frame in R Programming - subset() Function subset() function in R Programming Language is used to create subsets of a Data frame. This can also be used to drop columns from a data frame.Syntax: subset(df, expr)Parameters: df: Data frame usedexpr: Condition for subsetCreate Subsets of Data Frames in R Programming LanguageHere we will make sub 3 min read Like