Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function Last Updated : 01 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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 FUN: represents the function to be performed on factor levels Example 1: Python3 # Using mtcars dataset df <- data.frame(mtcars) # Factor levels on gear dffactors <- factor(mtcars$gear) # Output maximum hp of each factor i.e., gears by(df, dffactors, function(x){ m <- max(x$hp) }) Output: dffactors: 3 [1] 245 ------------------------------------------------------------ dffactors: 4 [1] 123 ------------------------------------------------------------ dffactors: 5 [1] 335 Example 2: Python3 # Using mtcars dataset df <- data.frame(mtcars) # Factor levels on gear dffactors <- factor(mtcars$gear) # Output mean of qsec of each factor i.e., gears by(df, dffactors, function(x){ m <- mean(x$qsec) }) Output: dffactors: 3 [1] 17.692 ------------------------------------------------------------ dffactors: 4 [1] 18.965 ------------------------------------------------------------ dffactors: 5 [1] 15.64 Comment More infoAdvertise with us Next Article Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function U utkarsh_kumar Follow Improve Article Tags : R Language R Functions R-Factors Similar Reads 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 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 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 Applying a Function over an Object in R Programming - sapply() Function sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set. Syntax: sapply(X, FUN) Parameters: X: A vector or an object FUN: Function applied to e 1 min read Performing Analysis of a Factor in R Programming - factanal() Function Factor Analysis also known as Exploratory Factor Analysis is a statistical technique used in R programming to identify the inactive relational structure and further, narrowing down a pool of variables to few variables. The main motive to use this technique is to find out which factor is most respons 2 min read Like