Apply a Function over a List of elements in R Programming - lapply() Function Last Updated : 24 Jun, 2022 Comments Improve Suggest changes Like Article Like Report lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.lapply(List, sum): Returns the sum of elements held by objects in the list, List.lapply(List, mean): Returns the mean of elements held by objects in the list, List.lapply(List, cumsum): Returns the cumulative sum of elements held by objects present inside the list, List. Syntax: lapply(list, func) Parameters: list: list of elements R - Apply a Function over a List of elementsExample 1: Basic example of lapply() Function in R programming R # R program to illustrate # lapply() function # Creating a matrix A = matrix(1:9, 3, 3) # Creating another matrix B = matrix(10:18, 3, 3) # Creating a list myList = list(A, B) # applying lapply() determinant = lapply(myList, det) print(determinant) Output: [[1]] [1] 0 [[2]] [1] 5.329071e-15Example 2: Apply a Function over a List of elements in R R # R program to illustrate # lapply() function # Creating a matrix A = matrix(1:9, 3, 3) # Creating another matrix B = matrix(10:18, 3, 3) # Creating a list myList = list(A, B) # applying lapply() sum = lapply(myList, sum) print(sum) Output: [, 1] [, 2] [, 3] [1, ] 1 4 7 [2, ] 2 5 8 [3, ] 3 6 9 [1] 28 80 162 [1] 6 120 504 Comment More infoAdvertise with us Next Article Apply a Function over a List of elements in R Programming - lapply() Function N nidhi_biet Follow Improve Article Tags : R Language R List-Function Similar Reads 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 Apply a Function over a Ragged Array in R Programming - tapply() Function tapply() function in R Language is used to apply a function over a subset of vectors given by a combination of factors Syntax: tapply(vector, factor, fun) Parameters: vector: Created Vector factor: Created Factor fun: Function to be applied Example 1: Python3 1== # R Program to apply a function # ov 1 min read Recursively apply a Function to a List in R Programming - rapply() function rapply() function in R Language is used to recursively apply a function to a list. Syntax: rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list")) Parameters: object: represents list or an expression f: represents function to be applied recursively classes: represents 3 min read Convert an Object to List in R Programming - as.list() Function as.list() function in R Programming Language is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and dataframes. Syntax: as.list(object) Parameters: object: Vector, Matrix, factor, or data frame R - as.list() Function ExampleExample 1: Converting Vector to list 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 Like