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().


Introduction:

#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){
  return(sum(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){
  x1-x2
}
b = 2

#with the second parameter as an option
apply(a,1,f2,x2=b)

#       [,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
apply(a,1,f2,x1=b)

# [,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().
1

View comments

Pricing of a financial product : A pricer of a call option.
Temporal network model - Barabási-Albert model with the library igraph
3
How to plot a network (package network) - Tip 2
1
Have you tried to understand your network? - Random generation of network models
Function apply() - Tip 1
1
Want to win "Guess who?" - Have an institutional neural network approach
How to choose your next holidays destination - Uniform distribution on a sphere
4
Generation of a normal distribution from "scratch" - The box-muller method
Generate stock option prices - How to simulate a Brownian motion
1
The consequence of merging insurance companies - Risk simulation and probability of ruin
Estimation of the number PI - A Monte Carlo simulation
The movement of a drunk guy - Random Walk and exponential regression
How does my computer know what language I am using? – An approach of statistical learning (Language, Computer Science)
How does my computer know what language I am using? – An approach of statistical learning (Language, Computer Science)
The fear-index: is the VIX efficient to be warned about high volatility? (Finance & Systematic Processus)
Who is the most complete athlete? – An insight with the Mahalanobis distance (sport & data analysis)
Who is the most complete athlete? – An insight with the Mahalanobis distance (sport & data analysis)
ProbaPerception: Introduction
Blog Archive
Translate
Translate
Loading