apply() Function in R with Example

The apply() in R is essentially allows us to cross the data in several ways to conceal the explicit uses of loop constructs. The apply() function can be utilized for an input matrix, array, or list.

apply() in R

The apply() is an inbuilt R function that returns a vector or an array or a list of values attained by applying a function to the margins of a matrix or an array. The apply() function takes the matrix or dataframe as an input and gives the vector, list, or array.

The apply() function could be used in one of the following cases.

  1. It can be an aggregating function, like mean or sum.
  2. It can help transform or subsetting functions.
  3. It can be used in vectorized functions, yielding more complex data structures like vectors, matrices, lists, and arrays.

The apply() functions create the base of more complex combinations of functions that help us to perform operations with little lines of code. More precisely, the apply() functions group is made up of lapply(), sapply(), vapply(), rapply(), mapply(), and tapply() functions.

Syntax

apply(X, MARGIN, FUN)

Parameters

The X is either an array or a matrix.

The MARGIN parameter can take a value or range between 1 and 2 to define where to apply the function.

The FUN parameter tells us which function to apply.

apply() function in R Matrix

To use the apply() function in R, let’s create a matrix (3 * 2).

outMatrix <- matrix(data = c(1:6), nrow = 3, ncol = 2)
print(outMatrix)

It will print the following matrix.

RScript Pro.R
    [,1] [,2]
[1,]  1    4
[2,]  2    5
[3,]  3    6

Use the apply() function and pass the outMatrix as first parameters, as a margin, and sum() as function. What it does is that it will return the sum of each column of the matrix.

outMatrix <- matrix(data = c(1:6), nrow = 3, ncol = 2)
print(outMatrix)
cat("After using apply() function", "\n")
apply_matrix <- apply(outMatrix, 2, sum)
print(apply_matrix)

Output

    [,1] [,2]
[1,]  1    4
[2,]  2    5
[3,]  3    6
After using apply() function
[1]   6    15

You can see that the sum of the first column elements is 6(1 + 2 + 3) and the second column elements is 15(4 + 5 + 6).

Here we applied a sum function to the column of the matrix using the apply() function.

This is the reason why the apply() function is useful because it will help us to calculate specific micro-operations from the large datasets. The apply() function makes the code so much faster to write, which is a huge advantage.

How to Use apply() function to Array in R

To use the apply() function in an array, create an array using vectors, and then use the apply() function to perform the specific operations.

Let’s define two vectors to form an array.

rv <- c(19, 21, 18)
rv2 <- c(11, 21, 46)

Now, to create an array, use the array() function and pass these two vectors.

rv <- c(19, 21, 18)
rv2 <- c(11, 21, 46)
ra <- array(c(rv, rv2), dim = c(2, 3, 1))
print(ra)

It will give us the following array.

, , 1

     [,1] [,2] [,3]
[1,]  19   18   21
[2,]  21   11   46

Now, use the apply() function to return the sum of the first row of the array and second row of the array.

rv <- c(19, 21, 18)
rv2 <- c(11, 21, 46)
ra <- array(c(rv, rv2), dim = c(2, 3, 1))
print(ra)
cat("After using apply() function", "\n")
apply_array <- apply(ra, 1, sum)
print(apply_array)

Output

, , 1

    [,1] [,2] [,3]
[1,] 19   18   21
[2,] 21   11   46

After using apply() function
[1]  58   78

You can see that the sum of the first row is 58, and the second array is 78.

The apply() function takes an array as the first argument, the second argument is 1 means we apply() the apply function to the row, and the sum is the function to execute on the row. If we pass the second argument as 2, then the sum function will apply to the columns.

Conclusion

The apply() allows us to make quick operations on vector, array, or matrix. The operations can be done on the rows, the columns, or even both of them.

That is it for apply() in R.

Leave a Comment