apply Function in R

The apply() function returns a vector or array or list of values obtained by applying a function to margins of an array or matrix. Basically, the apply() function is used to a evaluate a function over the margins of an array. It is most often used to apply a function to the rows or columns of a matrix. However, it can be used with general arrays, for example, to take the average of an array of matrices.

To get the help file for apply just type ?apply in the R console. To see the arguments of the apply function type str(apply).

 

?apply
str(apply)

Output:

function (X, MARGIN, FUN, …)

The arguments of apply() function are:

  1. X is an array
  2. MARGIN is an integer vector indicating which margins should be “retained”.
  3. FUN is a function to be applied
  4. … is for other arguments to be passed to FUN

Example:

Here a 4*5 matrix has been created and then mean of each column and rows has been computed below.

 

x <- matrix(1:20, 4, 5)

x
apply(x, 2, sum) # Take the mean of each column. 2 means column here
apply(x, 1, sum) # Take the mean of each row. 1 means row here.

Output:

> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20

> apply(x, 2, sum)
[1] 10 26 42 58 74
> apply(x, 1, sum)
[1] 45 50 55 60

Example 2:

 

x <- array(1:9,dim=c(3,3))

x
apply(x,1,function(x) x * 10)
apply(x,2,function(x) x * 10)

Output:

> x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> apply(x,1,function(x) x * 10)
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 40 50 60
[3,] 70 80 90
> apply(x,2,function(x) x * 10)
[,1] [,2] [,3]
[1,] 10 40 70
[2,] 20 50 80
[3,] 30 60 90

Example 3:

For a higher dimensional example, You can create an array and the compute the average of the matrices in the array. Check the following code to understand how it works.

 

set.seed(1)

x <- array(rnorm(2 * 2 * 3), c(2, 2, 3))
x
apply(x,1,sum)
apply(x,2,sum)

apply(x, c(1, 2), sum) # Here we are  preserving the first and second dimensions and collapsing the third dimension by taking the sum

Output:

> x
, , 1

[,1] [,2]
[1,] -0.6264538 -0.8356286
[2,] 0.1836433 1.5952808

, , 2

[,1] [,2]
[1,] 0.3295078 0.4874291
[2,] -0.8204684 0.7383247

, , 3

[,1] [,2]
[1,] 0.5757814 1.5117812
[2,] -0.3053884 0.3898432

> apply(x,1,sum)
[1] 1.442417 1.781235
> apply(x,2,sum)
[1] -0.6633781 3.8870304
>
> apply(x, c(1, 2), sum)
[1,] 0.2788353 1.163582
[2,] -0.9422134 2.723449

split

mapply