Avoid for-Loop in R? (Alternatives) | Using lapply Function Instead


In this R tutorial you’ll learn how to use the lapply function instead of for-loops.

The article will consist of this content:

If you want to know more about these topics, keep reading…


Example 1: Conventional for-Loop in R

In Example 1, I’ll show how to write and run a traditional for-loop in R.

Let’s assume that we want to run five iterations ranging from 1 to 5. In each iteration we want to print a sentence returning the current iteration index of the loop.

For this task, we can use the following R code:

for(i in 1:5) {              # Running for-loop
  print(paste("Iteration No.", i, "Created by for-Loop"))
# [1] "Iteration No. 1 Created by for-Loop"
# [1] "Iteration No. 2 Created by for-Loop"
# [1] "Iteration No. 3 Created by for-Loop"
# [1] "Iteration No. 4 Created by for-Loop"
# [1] "Iteration No. 5 Created by for-Loop"

As you can see, the RStudio console returned five sentences showing the index number of each iteration.

This code worked well. However, if not properly used for-loops can get very slow when applied to large data sets or in complex settings such as nested for-loops.

For that reason, it might make sense for you to avoid for-loops and to use functions such as lapply instead. This might speed up the R syntax and can save a lot of computational power!

The next example explains how to use the lapply function in R.


Example 2: Using lapply() Function Instead of for-Loop (Fast Alternative)

This Section explains how to create exactly the same output as in Example 1 using the lapply function in combination with the invisible function in R. Have a look at the following R syntax and its output:

invisible(lapply(1:5,        # Using lapply function
                 function(i) {
                   print(paste("Iteration No.", i, "Created by lapply Function"))
# [1] "Iteration No. 1 Created by lapply Function"
# [1] "Iteration No. 2 Created by lapply Function"
# [1] "Iteration No. 3 Created by lapply Function"
# [1] "Iteration No. 4 Created by lapply Function"
# [1] "Iteration No. 5 Created by lapply Function"

Exactly the same as in Example 1! Note that we have used a user-defined function within lapply. In this example, this user-defined function was very simple. However, you may make this function as complex as you want.


Video, Further Resources & Summary

Do you need more information on the content of this article? Then you might watch the following video of my YouTube channel. I show the R programming syntax of this tutorial in the video:


The YouTube video will be added soon.


Furthermore, you might read the other tutorials of this website.


To summarize: At this point you should know how to avoid for-loops in the R programming language. Don’t hesitate to let me know in the comments section, if you have any additional comments or 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.