How to use apply, sapply, lapply in R

In this tutorial some examples are shown on how to use the R commands apply, sapply and lapply. Mostly, theses commands work faster then loops in R.

apply

 

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

X:
array or matrix, with same type otherwise they will be converted

MARGIN:
‘1’ indicates rows
‘2’ indicates columns
‘c(1, 2)’ indicates rows and columns

Examples

# Data for examples
m=matrix(data=cbind(sin(1:10), cos(1:10), tan(1:10)), nrow=10, ncol=3)
n=matrix(cbind(sin(1:10), NA, tan(1:10)), nrow=10, ncol=3)

 

The function takes one argument, which is called x here. In this case x will be a single column of the matrix. Let’s check if x is a vector as expected.

# Is x a vector
apply(m, 2, function(x) is.vector(x))
#[1] TRUE TRUE TRUE

# equivalent
apply(m, 2, is.vector)

 

Calculating the column means of matrix m

# Mean of columns
apply(m, 2, mean)
#[1]  0.1411188 -0.1417448 -0.9016097

# equivalent
colMeans(m)

 

Calculating the row means of matrix n which contains missing values NA. The missing values shall be stripped before the computation proceeds (na.rm=T).

# Mean of rows with NA
apply(n, 1, mean, na.rm=T)

# equivalent
apply(n, 1, function(x) mean(x,na.rm=T))

 

Calculate the amount of all negative numbers in each column of matrix m

# Amount of negative numbers in each column
apply(m, 2, function(x) length(subset(x,x<0)))
#[1] 4 6 6

# equivalent
dat=c()
i=1
while(i<=3){
  dat=c(dat,length(subset(m[,i],m[,i]<0)))
i=i+1}
dat

 

sapply, lapply

lapply(X, FUN, ...)

X: a vector (atomic or list) or an ‘expression’ object.

lapply: returns a list of the same length as ‘X’,
sapply: returns a vector, matrix or an array

# sapply: returns a vector, matrix or an array
sapply(c(1:3), function(x) x^2)
[1] 1 4 9

# lapply: returns a list of the same length as ‘X’
lapply(c(1:3), function(x) x^2)
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

 

Examples

# Mean of columns
sapply(1:3, function(x) mean(m[,x]))
#[1]  0.1411188 -0.1417448 -0.9016097

#equivalent
sapply(1:3, function(x, y) mean(y[,x]), y=m)
apply(m, 2, mean)

 

Replace multiple values in a vector at once:

d1=cbind(c(1:5),c(1,5,2,7,14),c(11:15))
x=c(1,2,7,14)
y=c('eins','zwei','sieben','vierzehn')
d2=d1
sapply(1:length(x), function(i) {d2[d2==x[i]]<<-y[i]})
d2
#      [,1] [,2] [,3]
#[1,] "eins" "eins" "11"
#[2,] "zwei" "5" "12"
#[3,] "3" "zwei" "13"
#[4,] "4" "sieben" "vierzehn"
#[5,] "5" "vierzehn" "15"