Connect with us

Hi, what are you looking for?


Loop Functions In R Programming

In this tutorial, we going to learn the loop function, in another saying we going to learn apply,lapply,tapply, and mapply if you don’t know data types, you can read Data Types In R.

What Are Loop Functions?

You can operate on data types with the loop functions, these functions save you from creating a loop over and over again. It is also possible to apply the operations you want to all data with loop functions.

Apply Loop Function

The apply function is used to operate on a matrix or data frame, resulting in a vector, list, or array. The syntax is simple, the first argument is matrix or data frame, the second argument is used to select a column or row, and the last argument specifies the action to be taken.

x <- matrix(1:6 , ncol = 3 , nrow = 2)
apply(x , 1 , sum)

In here, we have gathered up the rows of the matrix. If we are going to operate on the columns, it is sufficient to write 2 instead of 1.

Lapply Loop Function

The lapply function is used to operate on a list, the output is the same length as the input. Each element is the result of applying the operation to the corresponding element of the list.

x <- list(c(1 , 2 , 3) , c(4 , 5 , 6) , c(7 , 8 , 9)
lapply(x , mean)

very similar to the apply syntax, a list is given to the first argument and 2nd argument the action to be taken on the is written. In the example here, each index of x will be averaged and written to the same index on the output list.

Mapply Loop Function

The mapply function applies the required operation to all indexes in the list one by one, it also applies to indexes that are in a vector, it has a multivariate structure.

mapply(rep, 1:4 , 1:4)

The operation here is applied to all indexes and too also the values inside the vector and a result is printed on the screen as below.

[1] 1

[1] 2 2

[1] 3 3 3

[1] 4 4 4 4
Tapply Function

The function tapply is used to manipulate the child vectors of a list or vector, also this function allows us to operate between different groups.

x <- c(runif(10) , runif(10) , runif(10))

c <- gl(3 , 10)

tapply(x , c , mean)
# We divided our vector into 3 groups and performed separate transactions for each group.

The usage can be a little more complicated, the first argument will be your string or vector to be operated with a group, the second argument is the factor of this vector, and the last operation is written.


Click to comment

Leave a Reply

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

You May Also Like


clean code is essential for updating scripts, adding new features to code, etc. So every developer, must learn how to clean the code. With...

Daily News

Cloud computing is a system that is often heard today and that most developers use at least once. In this article, we’ll look at...

Game Development

Visual Studio Code without a doubt, most popular and loved code editor in the code editor market. Many developers love working with Vs Code....

Daily News

GitHub Copilot was introduced last year and continues to make it easier for developers with nice updates to date. Although most people see Github...