# Apply Function to Each List Element in R (3 Examples)

This article demonstrates how to **apply a function such as mean() or sum() to each element of a list** in the R programming language.

The page consists of these topics:

Letâ€™s take a look at some R codes in action!

## Creation of Example Data

Weâ€™ll use the data below as basement for this R programming tutorial:

my_list <- list(1:6, # Create example list c(9, 1, 9, 5), 7:3) my_list # Print example list # [[1]] # [1] 1 2 3 4 5 6 # # [[2]] # [1] 9 1 9 5 # # [[3]] # [1] 7 6 5 4 3

The previous output of the RStudio console shows that our example data is a list object containing three list elements. Each of these list elements contains a vector of numerical values.

## Example 1: Calculate Mean of All List Elements Using lapply() Function

This example explains how to apply the mean function over all elements of a list in R.

For this task, we can use the lapply function as shown below:

my_list_mean1 <- lapply(my_list, mean) # Mean of all list elements my_list_mean1 # Return list of means # [[1]] # [1] 3.5 # # [[2]] # [1] 6 # # [[3]] # [1] 5

Have a look at the previous output of the RStudio console: We have created another list containing the mean of each list element of our input list.

## Example 2: Calculate Mean of All List Elements Using sapply() Function

Example 2 shows how to use the sapply function instead of the lapply function to compute the mean of every list item.

Have a look at the following R code and its output:

my_list_mean2 <- sapply(my_list, mean) # Mean of all list elements my_list_mean2 # Return vector of means # [1] 3.5 6.0 5.0

As you can see, the resulting mean values are exactly the same as in Example 1. However, the sapply function has returned our results as a vector instead of a list.

## Example 3: Calculate Sum of All List Elements Using lapply() Function

We can use the lapply and sapply functions in combination with other summary statistics (e.g. the standard deviation, variance, median, or the quantiles) as well.

In Example 3, Iâ€™ll explain how to calculate the sum of each list element using lapply and the sum function.

my_list_sum <- lapply(my_list, sum) # Sum of all list elements my_list_sum # Return list of sums # [[1]] # [1] 21 # # [[2]] # [1] 24 # # [[3]] # [1] 25

The previous R code has returned another list containing the sum of each list item.

## Video & Further Resources

I have recently released a video on my YouTube channel, which shows the R codes of this tutorial. You can find the video below:

In addition, you might have a look at some of the other articles on statisticsglobe.com. You can find a selection of articles below.

- Test If List Element Exists in R
- Remove Element from List in R (7 Example Codes)
- Select First Element of Nested List
- Index Element of List in R
- Useful Functions in R (Examples)
- Introduction to R Programming

In summary: In this tutorial, I have demonstrated how to **apply a function to each item of a list** in the R programming language. In case you have additional questions, donâ€™t hesitate to let me know in the comments section below.