In this tutorial, you will understand the lapply function and how to apply it to a list or vector in R. By following along, you will become proficient in using this critical function in the R programming language.
What is the lapply Function in R?
The lapply() is a function in the base package of R that applies a function to each element of a list or vector that returns a list with the same length as the input, but with the elements being the results of applying the function to the corresponding element of the input.
You can see the syntax of this function below.
lapply Function in R: The Syntax
lapply(DATA, FUN, ...)
Parameters:
- DATA: is a list or vector, data frame, …
- FUN: is the function to be applied to each element of DATA
How to use the lapply function in R?
lapply function with a vector
To use the lapply function, first, we need to create a vector and a function. You can create a fake vector and function as follows:
# Create a vector x <- c(5, 9, 6) # Create a function: square function func <- function(x){ x^2 }
We will apply the ‘func’ function to each element of the vector x and return a new vector with the square of each element:
# Create a vector x <- c(5, 9, 6) # Create a function: square function func <- function(x) { x^2 } # Calculate the square of each element lapply(x, func)
Output
[[1]]
[1] 25
[[2]]
[1] 81
[[3]]
[1] 36
lapply function with a list
Here, we will create a list for this example.
# Create a list x <- c(8, 5, 6, 2) # View a list x
Now, we will use the lapply function with this list, and log function in R. Check out the code example below
# Create a list x <- c(8, 5, 6, 2) # Calculate the square of each element lapply(x,log)
Output
[[1]]
[1] 2.079442
[[2]]
[1] 1.609438
[[3]]
[1] 1.791759
[[4]]
[1] 0.6931472
lapply function with a data frame
In this example, we have a data frame below
# Create a data frame df <- data.frame( Name = c("Linda R. Mancuso", "Mark C. Arnold", "Janice E. Clark"), ID = c(1, 2, 3), Math = c(8, 7, 9), English = c(7, 8, 9) ) # View a data frame df
We will use the lapply function in R to calculate the mean of the Math and English columns of the data frame above. See the example below.
# Create a data frame df <- data.frame( Name = c("Linda R. Mancuso", "Mark C. Arnold", "Janice E. Clark"), ID = c(1, 2, 3), Math = c(8, 7, 9), English = c(7, 8, 9) ) # Create a function to use the quick lapply function lapp <- function(x) { return(lapply(x, mean)) } # Calculate the mean cat("The mean of the Math columns is:\n") lapply(df$Math, mean) cat("The mean of the English columns is:\n") lapply(df$Math, mean)
Output
The mean of the Math columns is:
[[1]]
[1] 8
[[2]]
[1] 7
[[3]]
[1] 9
The mean of the English columns is:
[[1]]
[1] 8
[[2]]
[1] 7
[[3]]
[1] 9
Summary
In this article, we learned how to utilize the lapply function in R to apply a specific function to every element within a list or vector. If you have any questions or need further clarification, please leave a comment below.
We hope you have a great day!

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript