Function apply() – Tip 1

November 19, 2012

(This article was first published on ProbaPerception, and kindly contributed to R-bloggers)

The function apply() is certainly one of the most useful function. I was scared of it during a while and refused to use it. But it makes the code so much faster to write and so efficient that we can’t afford not using it. If you are like me, that you refuse to use apply because it is scary, read the following lines, it will help you. You want to know how to use apply() in general, with a home-made function or with several parameters ? Then, go to see the following examples.

The function apply() is known as extremely useful to improve the speed of our code when we want to operate some functions on a matrix or a vector. I use it, but I do not really know how to use it.

What is it?

apply() is a R function which enables to make quick operations on matrix, vector or array. The operations can be done on the lines, the columns or even both of them.

How does it work?

The pattern is really simple : apply(variable, margin, function).
variable is the variable you want to apply the function to.
margin specifies if you want to apply by row (margin = 1), by column (margin = 2), or for each element (margin = 1:2). Margin can be even greater than 2, if we work with variables of dimension greater than two.
function is the function you want to apply to the elements of your variable.

Because I think example is clearer than anything else, here is the most important example to understand the function apply().


#the matrix we will work on:
a = matrix(c(1:15), nrow = 5 , ncol = 3)

#will apply the function mean to all the elements of each row
apply(a, 1, mean)
# [1]  6  7  8  9 10

#will apply the function mean to all the elements of each column
apply(a, 2, mean)
# [1]  3  8 13

#will apply the function mean to all the elements of each column and of each row, ie each element
apply(a, 1:2, mean)
#     [,1] [,2] [,3]
# [1,]    1    6   11
# [2,]    2    7   12
# [3,]    3    8   13
# [4,]    4    9   14
# [5,]    5   10   15

We have just worked on the different margins to show the basic possibilities. But as I said we can also work on other variables such as an array of dimension 3:

#apply() for array of other dimension :
a = array(1:8, dim = c(2,2,2))
apply(a, 3, sum)

# , , 1
#       [,1] [,2]
# [1,]    1    3
# [2,]    2    4
# , , 2
#        [,1] [,2]
# [1,]    5    7
# [2,]    6    8

Use a home-made function:

We can also use our own function. For example, we reproduce the function sum (absolutely useless but let’s keep it simple!).

f1 = function(x){

apply(a, 1, f1)

Several parameters:
A function with several parameters. Here is the main reason why I was never using apply(), I did not know how to do when I had several parameters in a function. This is quite simple, we just have to specifiy in the parameter the variable which is constant.

f2 = function(x1,x2){
b = 2

#with the second parameter as an option

#       [,1] [,2] [,3] [,4] [,5]
# [1,]   -1    0    1    2    3
# [2,]    4    5    6    7    8
# [3,]    9   10   11   12   13
# [3,]   22   24   26   28   30

#with the first parameter as an option

# [,1] [,2] [,3] [,4] [,5]
# [1,]    1    0   -1   -2   -3
# [2,]   -4   -5   -6   -7   -8
# [3,]   -9  -10  -11  -12  -13

I hope these examples will help you. Personally I use the function apply() since I went through these few examples. Of course there are many other details which can be useful, but these first examples deal with the main useful possibilities of apply().

To leave a comment for the author, please follow the link and comment on their blog: ProbaPerception. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)