apply Function in R with Examples

In this tutorial, we will discuss about apply() function in R with some examples. The apply() function is available in base R package.

apply() function in R

The apply() function is the most popular function in R. The apply() function takes a matrix or array, an index and a function (built-in or user-defined) as inputs.

The general syntax of apply() function is

apply(X,MARGIN,FUN,...)

where

  • X: an array or matrix
  • MARGIN: a vector giving the subscripts which the function will be applied over. For matrix 1 indicate rows, 2 indicate columns, c(1,2) indicates both.
  • FUN: the function to be applied.
  • ...: optional argument to FUN.

The function apply(X, MARGIN, FUN) apply a function (FUN) to margins (MARGIN) of an array or matrix (X) and return a vector or array or list of values by applying a function to margins of an array or matrix.

The apply function extract each row or column of a matrix as a vector, one at a time and passes it to the FUN.

apply() function in R on matrix

Example 1: apply() function on rows of a Matrix

Suppose we have a matrix A as

$$ A= \begin{bmatrix} 12 & 14\\ 17 & 18 \\ 13 & 20 \end{bmatrix} $$

Create above matrix in R using matrix() function as:

A <- matrix(c(12, 14, 17, 18, 13, 20), nrow = 3)
A
     [,1] [,2]
[1,]   12   18
[2,]   14   13
[3,]   17   20

Suppose you wish to compute the row sums of matrix $A$. For this use apply() function on matrix A by setting MARGIN=1 and FUN = sum.

# compute row sums
apply(A, 1, sum)
[1] 30 27 37

Suppose you wish to compute the mean of each row of matrix $A$. For this use apply() function on matrix A by setting MARGIN=1 and FUN = s=mean.

# compute row means
apply(A, 1, mean)
[1] 15.0 13.5 18.5

Note that for the calculation of row sums and row means of matrix or array, the more efficient way is to use rowSums() and rowMeans() function respectively.

Example 2: apply() function on columns of a Matrix

Suppose you wish to compute the column means of matrix $A$. For this use apply() function on matrix A by setting MARGIN=2 and FUN = mean.

# Compute column means
apply(A, 2, mean) 
[1] 14.33333 17.00000

Note that for the calculation of column sums and column means of matrix or array, the more efficient way is to use columnSums() and columnMeans() function respectively.

Suppose you wish to compute the standard deviation for each column of matrix $A$. For this use apply() function on matrix A by setting MARGIN=2 and FUN = sd.

# Compute column standard deviation
apply(A, 2, sd)
[1] 2.516611 3.605551

Example 3: apply() function with optional argument

The apply() function allows us to pass an additional argument to the function.

Suppose you wish to compute the column means of matrix $A$. For this use apply() function on matrix A by setting MARGIN=2 and FUN = mean.

B <- matrix(c(10, 20, 30, NA), nrow = 2)
B
     [,1] [,2]
[1,]   10   30
[2,]   20   NA
# Compute row means
apply(B, 1, mean, na.rm = TRUE)  
[1] 20 20
# Compute column means
apply(B, 2, mean, na.rm = TRUE) 
[1] 15 30

Note that we can use optional argument ... to the function in apply() function, like na.rm=TRUE for the mean() function.

Example 4: apply() function on Matrix with user-defined function

Suppose we want to calculate standard error of each column of given matrix. First define a user-defined function for standard error as follows:

std.error <- function(x) {
  sd(x) / sqrt(length(x))
}

To calculate standard error for each column of a matrix A, we use apply() function with FUN as a user-defined function std.error as follows:

# compute the standard error for columns of A
apply(A, 2, std.error)
[1] 1.452966 2.081666

apply() function in R on array

Example 5: apply() function on array

To understand the use of apply() function on array, let us create an array of dimension $2\times 3\times 2$ and store it in myarray.

myarray <- array(1:12, dim = c(2, 3, 2))
myarray
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12
# Compute sum of the rows
apply(myarray, 1, sum) 
[1] 36 42

The result is the sum of all the elements of 1st row of Array myarray

1+3+5+7+9+11 = 36

2+4+6+8+10+12 =42.

# Compute sum of the columns
apply(myarray, 2, sum)
[1] 18 26 34

The result is the sum of all the elements of 1st row of Array myarray.

1+2+7+8 = 18

3+4+9+10 = 26

5+6+11+12 =34

# Compute sum of the rows as well as columns
apply(myarray, c(1, 2), sum)
     [,1] [,2] [,3]
[1,]    8   12   16
[2,]   10   14   18

The result is the sum of all corresponding elements of array (for rows and columns). (i.e. 1+7=8, 3+9=12, etc.)

Similarly, one can use other built-in functions or user-defined functions in apply() function.

apply() function in R on data frame

Example 6: apply() function on data frame

apply() function can also be used on data frame. To understand the use of apply function on data frame, let us create a small data frame df as follows:

x<-1:6
y<-c("S","F","F","S","F","S")
z<-c(10,12,13,15,17,18)
# create a data frame
df <- data.frame(x=x,y=y,z=z)
df
  x y  z
1 1 S 10
2 2 F 12
3 3 F 13
4 4 S 15
5 5 F 17
6 6 S 18
# compute means of 1st and 3rd column of df
apply(df[,c(1,3)],2,mean)
       x        z 
 3.50000 14.16667 

Basically apply() function is for matrix and array, but you can use it on columns of data frame also. Don't use apply() function on rows of data frames, because their types and units of measurements may be different.

Endnote

In this tutorial you learned about apply() function in R and how to use apply() function on matrix and array with illustration.

Learn more about functions in R, refer to the following tutorials:

Hopefully you enjoyed learning this tutorial on apply() function in R. Hope the content is more than sufficient to understand apply() function in R.

Leave a Comment