5.4 Apply Family

Apply family is a very convenient tools to loop over data structure (vector, array, matrix and list). The most useful for our purposes are:

  1. apply(),
  2. sapply(), and
  3. lapply().

5.4.1 apply

We will to apply a function to each row or each column on a matrix. It will apply a function by-column (=2) or by-row (=1).

x <- matrix(1:9, nrow = 3, ncol=3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
apply(x,1,mean) # average each row
## [1] 4 5 6
apply(x,2,mean) # average each column
## [1] 2 5 8

5.4.2 sapply and lapply

Sometimes, we will apply a function repeatedly on each element of a vector (or a list). Then sapply is more convenient then a for loop.

Consider the following for loop that returns the square of each element

x <- c(1,3,5)
for (i in 1:length(x)) {
  x[i] <- x[i]^2
}
x
## [1]  1  9 25

We can do the same using sapply() with much cleaner code.

x <- c(1,3,5)
x <- sapply(x, function(x) x^2)
x
## [1]  1  9 25

lapply() is the same as sapply() but we end up with a list instead of vector. We can use unlist to get back the same result as sapply().

x <- c(1,3,5)
x <- lapply(x, function(x) x^2)
x
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 9
## 
## [[3]]
## [1] 25
x <- unlist(x)