How to Use the foreach() Function in R


Often you may want to use a “foreach” style loop in which you can perform a specific task on each item in a loop in R.

Fortunately this is easy to do by using the foreach() function from the foreach package in R, which is designed to perform this exact task.

Here are three common ways to use the foreach() function in practice:

Method 1: Use foreach() with One Variable

foreach(i=1:5) %do%
  print(i/5)

This particular example uses a foreach() function to loop through the values 1 through 5, then print out the result of each value divided by 5.

Method 2: Use foreach() with Multiple Variables

foreach(i=1:3, j=1:3) %do%
  print(i+j)

This particular example uses a foreach() function to loop through the values of two different variables and then print the result of the sum of the two variables.

Method3 : Use foreach() with Matrix

foreach(i=1:ncol(mat), .combine=c) %do%
  mean(mat[,i])

This particular example uses a foreach() function to loop through the columns of a matrix named mat and then return the mean value of each column.

The following examples show how to use each of these methods in practice.

Note: Before using the foreach() function, you may need to first install the foreach package by using the following syntax:

install.packages('foreach')

Once the foreach package is installed, you can use the foreach() function.

Example 1: Use foreach() with One Variable in R

Suppose that we would like to use a foreach() function to loop through the values of some vector and print each element of the vector divided by 5.

We can use the following syntax to do so:

library(foreach)

foreach(i=1:5) %do%
  print(i/5)

[1] 0.2
[1] 0.4
[1] 0.6
[1] 0.8
[1] 1
[[1]]
[1] 0.2

[[2]]
[1] 0.4

[[3]]
[1] 0.6

[[4]]
[1] 0.8

[[5]]
[1] 1

This returns each element of the vector divided by 5.

Example 2: Use foreach() with Multiple Variables in R

Suppose that we would like to use a foreach() function to loop through the values of two vectors and print the sum of each corresponding element in each vector.

We can use the following syntax to do so:

library(foreach)

foreach(i=1:3, j=1:3) %do% 
  print(i+j)

[1] 2
[1] 4
[1] 6
[[1]]
[1] 2

[[2]]
[1] 4

[[3]]
[1] 6

This returns the sum of each corresponding element between the two variables, i and j, that we specified.

Example 3: Use foreach() with Matrix in R

Suppose that we would like to use a foreach() function to loop through the columns of a matrix in R and then print the sum of the values in each column.

We can use the following syntax to do so:

library(foreach)

#create matrix
mat <- matrix(1:9, nrow=3)

#view matrix
mat

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

#calculate mean of each column of matrix
foreach(i=1:ncol(mat), .combine=c) %do% 
  mean(mat[,i])

[1] 2 5 8

This returns the mean of each column of the matrix.

For example, we can see:

  • The mean of the first column is 2.
  • The mean of the second column is 5.
  • The mean of the third column is 8.

Feel free to replace mean() with any other function that you would like to calculate a different summary statistic instead.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use slice_min() in dplyr
How to Use the pull() Function in dplyr
How to Use top_n() in dplyr
How to Rename Columns Using dplyr

Leave a Reply

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