apply Functions in R (6 Examples) | lapply, sapply, vapply, tapply & mapply

 

In this article you’ll learn how to use the family of apply functions in the R programming language.

The content of the post looks as follows:

So without further additions, let’s dive right into the examples.

 

Creation of Example Data

As a first step, let’s create some exemplifying data in R. For some of the apply functions, we’ll need a data frame:

my_data <- data.frame(x1 = 1:5,            # Create example data
                      x2 = 2:6,
                      x3 = 3)
my_data                                    # Print example data
#   x1 x2 x3
# 1  1  2  3
# 2  2  3  3
# 3  3  4  3
# 4  4  5  3
# 5  5  6  3

As you can see based on the previous output of the RStudio console, our example data frame contains five rows and three numeric columns.

For other commands of the apply family, we’ll need a list:

my_list <- list(1:5,                       # Create example list
                letters[1:3],
                777)
my_list                                    # Print example list
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "a" "b" "c"
# 
# [[3]]
# [1] 777

Our list consists of three list elements. The list elements at index positions one and three are numeric and the second list element is a character vector.

 

Example 1: apply() Function

This Example explains how to use the apply() function. The apply function takes data frames as input and can be applied by the rows or by the columns of a data frame. First, I’ll show how to use the apply function by row:

apply(my_data, 1, sum)                     # Using apply function
# 6  8 10 12 14

As you can see based on the previous R code, we specified three arguments within the apply function:

  • The name of our data frame (i.e. my_data).
  • Whether we want to use the apply function by rows or by columns. The value 1 indicates that we are using apply by row.
  • The function we want to apply to each row (i.e. the sum function).

In other words: The previous R syntax computed the row sums of each row of our data frame.

Now, let’s use the apply function by column:

apply(my_data, 2, sum)
# x1 x2 x3 
# 15 20 15

As you can see based on the previous output of the RStudio console, the sum of variable x1 is 15, the sum of variable x2 is 20, and the sum of variable x3 is also 15.

Note that we only changed the value 1 to the value 2 in order to use the apply function by column. The remaining R code was kept exactly the same.

However, the family of apply commands contains many different functions that can be selected depending on your input data and the output you want to generate. The next functions are using lists as input data…

 

Example 2: lapply() Function

In Example 2, I’ll illustrate how to use the lapply function. The l in front of apply stands for “list”.

Within the lapply function, we simply need to specify the name of our list (i.e. my_list) and the function we want to apply to each list element. In the following example, I’m returning the length of each list element:

lapply(my_list, length)                    # Using lapply function
# [[1]]
# [1] 5
# 
# [[2]]
# [1] 3
# 
# [[3]]
# [1] 1

The previous output shows our result: The first list element has a length of 5, the second list element has a length of 3, and the third list element has a length of 1.

 

Example 3: sapply() Function

As you have seen in the previous example, the lapply function returns a very complex output, which might be hard to read. The sapply function (s stands for simple) therefore provides a simpler output than lapply:

sapply(my_list, length)                    # Using sapply function
# 5 3 1

The result is the same as in Example 2, but this time the output is shown in the vector format.

 

Example 4: vapply() Function

The vapply function is very similar compared to the sapply function, but when using vapply you need to specify the output type explicitly. In this example, we’ll return an integer:

vapply(my_list, length, integer(1))        # Using vapply function
# 5 3 1

 

Example 5: tapply() Function

The tapply function is another command of the apply family, which is used for vector inputs. Typically, you need some values…

input_values <- 1:10                       # Create example values
input_values
# 1  2  3  4  5  6  7  8  9 10

…and a factor, which is grouping these values:

input_factor <- rep(letters[1:5], 2)       # Create example factor
input_factor
# "a" "b" "c" "d" "e" "a" "b" "c" "d" "e"

Now, we can us the tapply function to get (for instance) the sum of each group:

tapply(input_values, input_factor, sum)    # Using tapply function
# a  b  c  d  e 
# 7  9 11 13 15

 

Example 6: mapply() Function

Another function that is used for vectors is mapply. The mapply function can be used as shown below:

mapply(rep, times = 1:5, letters[1:5])     # Using mapply function
# [[1]]
# [1] "a"
# 
# [[2]]
# [1] "b" "b"
# 
# [[3]]
# [1] "c" "c" "c"
# 
# [[4]]
# [1] "d" "d" "d" "d"
# 
# [[5]]
# [1] "e" "e" "e" "e" "e"

 

Video, Further Resources & Summary

This tutorial explained how to use different functions of the apply family. As you have seen, the apply functions can be used instead of for-loops and are often a faster alternative.

Please note that the apply functions (except vapply) are not type-safe. This can lead to problems when they are used within functions. For these cases, the map_type() functions of the purrr package might be a better choice.

If you need more explanations on the R codes of this tutorial, you may have a look at the following video of my YouTube channel. In the video, I show the R code of this tutorial and give further explanations on the usage of apply functions in R.

 

The YouTube video will be added soon.

 

In addition, I can recommend reading some of the related posts on this homepage. I have released several articles already:

 

In summary: You learned on this page how to use different apply commands in R programming. Please let me know in the comments, in case you have additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top