Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

apply Functions

1 What are apply functions?


Apply functions are a family of functions in base R which allow you to repetitively perform an
action on multiple chunks of data. An apply function is essentially a loop, but run faster than
loops and often require less code.
The apply functions that this chapter will address are apply, lapply, sapply, vapply, tapply, and
mapply. There are so many different apply functions because they are meant to operate on
different types of data.

2 The apply function


First, let’s go over the basic apply function. You can use the help section to get a description of
this function.

?apply

the apply function looks like this: apply(X, MARGIN, FUN).

 X is an array or matrix (this is the data that you will be performing the function on)
 Margin specifies whether you want to apply the function across rows (1) or columns (2)
 FUN is the function you want to use

2.1 apply examples
my.matrx is a matrix with 1-10 in column 1, 11-20 in column 2, and 21-30 in column 3. my.matrx
will be used to show some of the basic uses for the apply function.

my.matrx <- matrix(c(1:10, 11:20, 21:30), nrow = 10, ncol = 3)


my.matrx
## [,1] [,2] [,3]
## [1,] 1 11 21
## [2,] 2 12 22
## [3,] 3 13 23
## [4,] 4 14 24
## [5,] 5 15 25
## [6,] 6 16 26
## [7,] 7 17 27
## [8,] 8 18 28
## [9,] 9 19 29
## [10,] 10 20 30

2.1.1 Example 1: Using apply to find row sums


What if I wanted to summarize the data in matrix m by finding the sum of each row? The
arguments are X = m, MARGIN = 1 (for row), and FUN = sum

apply(my.matrx, 1, sum)
## [1] 33 36 39 42 45 48 51 54 57 60

The apply function returned a vector containing the sums for each row.

2.1.2 Example 2: Creating a function in the arguments


What if I wanted to be able to find how many datapoints (n) are in each column of m? I can use
the length function to do this. Because we are using columns, MARGIN = 2.

apply(my.matrx, 2, length)
## [1] 10 10 10

What if instead, I wanted to find n-1 for each column? There isn’t a function in R to do this
automatically, so I can create my own function. If the function is simple, you can create it right
inside the arguments for apply. In the arguments I created a function that returns length - 1.

apply(my.matrx, 2, function (x) length(x)-1)


## [1] 9 9 9

As you can see, the function correctly returned a vector of n-1 for each column.

2.1.3 Example 3: Using a function defined outside of


apply
If you don’t want to write a function inside of the arguments, you can define the function outside
of apply, and then use that function in apply later. This may be useful if you want to have the
function available to use later. In this example, a function to find standard error was created, then
passed into an apply function.

st.err <- function(x){


sd(x)/sqrt(length(x))
}
apply(my.matrx,2, st.err)
## [1] 0.9574271 0.9574271 0.9574271

2.1.4 Example 4: Transforming data


Now for something a little different. In the previous examples, apply was used to summarize over
a row or column. It can also be used to repeat a function on cells within a matrix. In this example,
the apply function is used to transform the values in each cell. Pay attention to the MARGIN
argument. If you set the MARGIN to 1:2 it will have the function operate on each cell.

my.matrx2 <- apply(my.matrx,1:2, function(x) x+3)


my.matrx2
## [,1] [,2] [,3]
## [1,] 4 14 24
## [2,] 5 15 25
## [3,] 6 16 26
## [4,] 7 17 27
## [5,] 8 18 28
## [6,] 9 19 29
## [7,] 10 20 30
## [8,] 11 21 31
## [9,] 12 22 32
## [10,] 13 23 33

2.1.5 Example 5: Vectors?
The previous examples showed several ways to use the apply function on a matrix. But what if I
wanted to loop through a vector instead? Will the apply function work?

vec <- c(1:10)


vec
## [1] 1 2 3 4 5 6 7 8 9 10
apply(vec, 1, sum)

If you run this function it will return the error: Error in apply(v, 1, sum) : dim(X) must have a
positive length. As you can see, this didn’t work because apply was expecting the data to have at
least two dimensions. If your data is a vector you need to use lapply, sapply, or vapply instead.
3 lapply, sapply, and vapply
lapply, sapply, and vapply are all functions that will loop a function through data in a list or vector.
First, try looking up lapply in the help section to see a description of all three function.

?lapply

Here are the agruments for the three functions:

 lapply(X, FUN, …)
 sapply(X, FUN, …, simplify = TRUE, USE.NAMES = TRUE)
 vapply(X, FUN, FUN.VALUE, …, USE.NAMES = TRUE)

In this case, X is a vector or list, and FUN is the function you want to use. sapply and vapply
have extra arguments, but most of them have default values, so you don’t need to worry about
them. However, vapply requires another agrument called FUN.VALUE, which we will look at
later.

3.0.1 Example 1: Getting started with lapply


Earlier, we created the vector v. Let’s use that vector to test out the lapply function.

lapply(vec, sum)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] 3
##
## [[4]]
## [1] 4
##
## [[5]]
## [1] 5
##
## [[6]]
## [1] 6
##
## [[7]]
## [1] 7
##
## [[8]]
## [1] 8
##
## [[9]]
## [1] 9
##
## [[10]]
## [1] 10

This function didn’t add up the values like we may have expected it to. This is because lapply
applies treats the vector like a list, and applies the function to each point in the vector.
Let’s try using a list instead

A<-c(1:9)
B<-c(1:12)
C<-c(1:15)
my.lst<-list(A,B,C)
lapply(my.lst, sum)
## [[1]]
## [1] 45
##
## [[2]]
## [1] 78
##
## [[3]]
## [1] 120

This time, the lapply function seemed to work better. The function summed each vector in the list
and returned a list of the 3 sums.

3.0.2 Example 2: sapply
sapply works just like lapply, but will simplify the output if possible. This means that instead of
returning a list like lapply, it will return a vector instead if the data is simplifiable.

sapply(vec, sum)
## [1] 1 2 3 4 5 6 7 8 9 10
sapply(my.lst, sum)
## [1] 45 78 120

See how these two examples gave the same answers, but returned a vector instead?

3.0.3 Example 3: vapply
vapply is similar to sapply, but it requires you to specify what type of data you are expecting the
arguments for vapply are vapply(X, FUN, FUN.VALUE). FUN.VALUE is where you specify the
type of data you are expecting. I am expecting each item in the list to return a single numeric
value, so FUN.VALUE = numeric(1).

vapply(vec, sum, numeric(1))


## [1] 1 2 3 4 5 6 7 8 9 10
vapply(my.lst, sum, numeric(1))
## [1] 45 78 120

If your function were to return more than one numeric value, FUN.VALUE = numeric(1) will cause
the function to return an error. This could be useful if you are expecting only one result per
subject.

#vapply(my.lst, function(x) x+2, numeric(1))

3.0.4 Example 4: Transforming data with sapply


Like apply, these functions can also be used for transforming data inside the list

my.lst2 <- sapply(my.lst, function(x) x*2)


my.lst2
## [[1]]
## [1] 2 4 6 8 10 12 14 16 18
##
## [[2]]
## [1] 2 4 6 8 10 12 14 16 18 20 22 24
##
## [[3]]
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30

3.0.5 Which function should I use, lapply, sapply, or


vapply?
If you are trying to decide which of these three functions to use, because it is the simplest, I
would suggest to use sapply if possible. If you do not want your results to be simplified to a
vector, lapply should be used. If you want to specify the type of result you are expecting, use
vapply.

4 tapply
Sometimes you may want to perform the apply function on some data, but have it separated by
factor. In that case, you should use tapply. Let’s take a look at the information for tapply.

?tapply

The arguments for tapply are tapply(X, INDEX, FUN). The only new argument is INDEX, which is
the factor you want to use to separate the data.

4.0.1 Example 1: Means split by condition


First, let’s create data with an factor for indexing. Dataset t will be created by adding a factor to
matrix m and converting it to a dataframe.

tdata <- as.data.frame(cbind(c(1,1,1,1,1,2,2,2,2,2), my.matrx))


colnames(tdata)
## [1] "V1" "V2" "V3" "V4"

Now let’s use column 1 as the index and find the mean of column 2

tapply(tdata$V2, tdata$V1, mean)


## 1 2
## 3 8

4.0.2 Example 2: Combining functions


You can use tapply to do some quick summary statistics on a variable split by condition. In this
example, I created a function that returns a vector ofboth the mean and standard deviation. You
can create a function like this for any apply function, not just tapply.

summary <- tapply(tdata$V2, tdata$V1, function(x) c(mean(x), sd(x)))


summary
## $`1`
## [1] 3.000000 1.581139
##
## $`2`
## [1] 8.000000 1.581139
5 mapply
the last apply function I will cover is mapply.

?mapply

the arguments for mapply are mapply(FUN, …, MoreArgs = NULL, SIMPLIFY = TRUE,
USE.NAMES = TRUE). First you list the function, followed by the vectors you are using the rest
of the arguments have default values so they don’t need to be changed for now. When you have
a function that takes 2 arguments, the first vector goes into the first argument and the second
vector goes into the second argument.

5.0.1 Example 1: Understanding mapply


In this example, 1:9 is specifying the value to repeat, and 9:1 is specifying how many times to
repeat. This order is based on the order of arguments in the rep function itself.

mapply(rep, 1:9, 9:1)


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

5.0.2 Example 2: Creating a new variable


Another use for mapply would be to create a new variable. For example, using dataset t, I could
divide one column by another column to create a new value. This would be useful for creating a
ratio of two variables as shown in the example below.

tdata$V5 <- mapply(function(x, y) x/y, tdata$V2, tdata$V4)


tdata$V5
## [1] 0.04761905 0.09090909 0.13043478 0.16666667 0.20000000 0.23076923
## [7] 0.25925926 0.28571429 0.31034483 0.33333333

5.0.3 Example 3: Saving data into a premade vector


When using an apply family function to create a new variable, one option is to create a new
vector ahead of time with the size of the vector pre-allocated. I created a numeric vector of length
10 using the vector function. The arguments for the vector function are vector(mode, length).
Inside mapply I created a function to multiple two variables together. The results of the mapply
function are then saved into the vector.

new.vec <- vector(mode = "numeric", length = 10)


new.vec <- mapply(function(x, y) x*y, tdata$V3, tdata$V4)
new.vec
## [1] 231 264 299 336 375 416 459 504 551 600

6 Using apply functions on real


datasets
This last section will be a few examples of using apply functions on real data.This section will
make use of the MASS package, which is a collection of publicly available datasets. Please
install MASS if you do not already have it. If you do not have MASS installed, you can
uncomment the code below.

#install.packages("MASS")
library(MASS)

load the state dataset. It contains information about all 50 states


data(state)

Let’s look at the data we will be using. We will be using the state.x77 dataset

head(state.x77)
## Population Income Illiteracy Life Exp Murder HS Grad Frost
## Alabama 3615 3624 2.1 69.05 15.1 41.3 20
## Alaska 365 6315 1.5 69.31 11.3 66.7 152
## Arizona 2212 4530 1.8 70.55 7.8 58.1 15
## Arkansas 2110 3378 1.9 70.66 10.1 39.9 65
## California 21198 5114 1.1 71.71 10.3 62.6 20
## Colorado 2541 4884 0.7 72.06 6.8 63.9 166
## Area
## Alabama 50708
## Alaska 566432
## Arizona 113417
## Arkansas 51945
## California 156361
## Colorado 103766
str(state.x77)
## num [1:50, 1:8] 3615 365 2212 2110 21198 ...
## - attr(*, "dimnames")=List of 2
## ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
## ..$ : chr [1:8] "Population" "Income" "Illiteracy" "Life Exp" ...

All the data in the dataset happens to be numeric, which is necessary when the function inside
the apply function requires numeric data.

6.0.1 Example 1: using apply to get summary data


You can use apply to find measures of central tendency and dispersion

apply(state.x77, 2, mean)
## Population Income Illiteracy Life Exp Murder HS Grad
## 4246.4200 4435.8000 1.1700 70.8786 7.3780 53.1080
## Frost Area
## 104.4600 70735.8800
apply(state.x77, 2, median)
## Population Income Illiteracy Life Exp Murder HS Grad
## 2838.500 4519.000 0.950 70.675 6.850 53.250
## Frost Area
## 114.500 54277.000
apply(state.x77, 2, sd)
## Population Income Illiteracy Life Exp Murder
## 4.464491e+03 6.144699e+02 6.095331e-01 1.342394e+00 3.691540e+00
## HS Grad Frost Area
## 8.076998e+00 5.198085e+01 8.532730e+04

6.0.2 Example 2: Saving the results of apply


In this, I created one function that gives the mean and SD, and another that give min, median,
and max. Then I saved them as objects that could be used later.

state.summary<- apply(state.x77, 2, function(x) c(mean(x), sd(x)))


state.summary
## Population Income Illiteracy Life Exp Murder HS Grad
Frost
## [1,] 4246.420 4435.8000 1.1700000 70.878600 7.37800 53.108000
104.46000
## [2,] 4464.491 614.4699 0.6095331 1.342394 3.69154 8.076998
51.98085
## Area
## [1,] 70735.88
## [2,] 85327.30
state.range <- apply(state.x77, 2, function(x) c(min(x), median(x),
max(x)))
state.range
## Population Income Illiteracy Life Exp Murder HS Grad Frost Area
## [1,] 365.0 3098 0.50 67.960 1.40 37.80 0.0 1049
## [2,] 2838.5 4519 0.95 70.675 6.85 53.25 114.5 54277
## [3,] 21198.0 6315 2.80 73.600 15.10 67.30 188.0 566432
6.0.3 Example 3: Using mapply to compute a new
variable
In this example, I want to find the population density for each state. In order to do this, I want to
divide population by area. state.area and state.x77 are not from the same dataset, but that is fine
as long as the vectors are the same length and the data is in the same order. Both vectors are
alphabetically by state, so mapply can be used.

population <- state.x77[1:50]


area <- state.area
pop.dens <- mapply(function(x, y) x/y, population, area)
pop.dens
## [1] 0.070045922 0.000618899 0.019419010 0.039733353 0.133578671
## [6] 0.024374802 0.618886005 0.281477880 0.141342213 0.083752293
## [11] 0.134573643 0.009729885 0.198528369 0.146399934 0.050826079
## [16] 0.027715647 0.083847011 0.078437030 0.031853078 0.389713529
## [21] 0.704129829 0.156503367 0.046640815 0.049061112 0.068406854
## [26] 0.005070070 0.019993008 0.005337434 0.087274291 0.935809086
## [31] 0.009402791 0.364611909 0.103468604 0.009014364 0.260419194
## [36] 0.038830647 0.023551005 0.261619571 0.766886326 0.090677830
## [41] 0.008838761 0.098783259 0.045773344 0.014166941 0.049120616
## [46] 0.122038466 0.052190873 0.074397254 0.081721694 0.003840105

6.0.4 Example 4: Using tapply to explore population by


region
In this example, I want to find out some information about the population of states split by region.
state.region is a factor with four levels: Northeast, South, North Central, and West. For each
region, I want the minimum, median, and maximum populations.
region.info <- tapply(population, state.region, function(x) c(min(x),
median(x), max(x)))
region.info
## $Northeast
## [1] 472 3100 18076
##
## $South
## [1] 579.0 3710.5 12237.0
##
## $`North Central`
## [1] 637 4255 11197
##
## $West
## [1] 365 1144 21198

You might also like