Get or Set Levels of a Factor in R Programming - levels() Function Last Updated : 16 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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")); gender # Calling levels() function # to get the levels levels(gender) Output: [1] female male male female Levels: female male [1] "female" "male" Example 2: Python3 1== # R program to set levels of a factor # Creating a factor gender <- factor(c("female", "male", "male", "female")); gender # Calling levels() function # to set the levels levels(gender) <- c("abc", "def") gender Output: [1] female male male female Levels: female male [1] abc def def abc Levels: abc def Comment More infoAdvertise with us Next Article Get or Set Levels of a Factor in R Programming - levels() Function N nidhi_biet Follow Improve Article Tags : R Language R Factor-Function Similar Reads Get the Number of Levels of a Factor in R Programming - nlevels() Function nlevels() function in R Language is used to get the number of levels of a factor. Syntax: nlevels(x) Parameters: x: Factor Object Example 1: Python3 1== # R program to get the number # of levels of a factor # Creating a factor x <- gl(3, 2) x # Calling nlevels() function # to get the number of le 1 min read Generate Factors with specified Levels in R Programming - gl() Function gl() function in R Language is used to generate factors by specifying the pattern of their levels. Syntax: gl(x, k, length, labels, ordered) Parameters: x: Number of levels k: Number of replications length: Length of result labels: Labels for the vector(optional) ordered: Boolean value to order the 2 min read Get or Set Dimensions of a Matrix in R Programming - dim() Function The dim() function in R is used to get or set the dimensions of an object. This function is particularly useful when working with matrices, arrays, and data frames. Below, we will discuss how to use dim() to both retrieve and set dimensions, with examples for better understanding.Syntax: dim(x)Param 2 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 Removing Levels from a Factor in R Programming - droplevels() Function 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 b 2 min read Like