Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Introduction to the Apply Function in R

The are several different types of apply functions within R. They are lapply, sapply, mapply, rapply, vapply, and (of course) the apply function itself.

· Big Data Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

The `apply` function returns an array, vector, or list of values by applying the function to margins of any array or matrix.

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

• `X` is an array or matrix.

• `MARGIN` is a variable that determines whether function needs to apply on rows (`MARGIN=1`) or columns (`MARGIN=2`) or both (`MARGIN=c(1,2)`).

• `FUN` is a function to be applied, such as mean, sum, user defined function, etc.

``````#Create Matrix With 2 Columns and 10 Rows
matrix.a <- matrix(c(1:20),ncol = 2)

#Print Matrix
matrix.a

#Apply Mean function row wise (MARGIN=1)
apply(matrix.a,MARGIN = 1,mean)

#Apply Mean function column wise (MARGIN=2)
apply(matrix.a,MARGIN = 2,mean)``````

Output:

``````> matrix.a <- matrix(c(1:20),ncol = 2)
> matrix.a
[,1] [,2]
[1,]    1   11
[2,]    2   12
[3,]    3   13
[4,]    4   14
[5,]    5   15
[6,]    6   16
[7,]    7   17
[8,]    8   18
[9,]    9   19
[10,]   10   20
> apply(matrix.a,MARGIN = 1,mean)
[1]  6  7  8  9 10 11 12 13 14 15
> apply(matrix.a,MARGIN = 2,mean)
[1]  5.5 15.5``````

`lapply` returns a list of the same length as `X`. Each element is the result of applying `FUN` to the corresponding element `X`.

`lapply (X,FUN,..)`:

• `X` is an list.

• `FUN` is a function to be applied.

``````#Create a List
list.a <- list(c(1,1),c(3,3),c(4,5))

#Print a List
list.a

#Apply mean function
lapply(list.a,mean)``````

Output:

``````> list.a <- list(c(1,1),c(3,3),c(4,5))
> list.a
[[1]]
[1] 1 1

[[2]]
[1] 3 3

[[3]]
[1] 4 5

> lapply(list.a,mean)
[[1]]
[1] 1

[[2]]
[1] 3

[[3]]
[1] 4.5``````

`sapply` is similar to `lapply`, but it returns the result in the form of a matrix or vector, if appropriate.

`sapply (X,FUN,..)`:

• `X` is a list.

• `FUN` is a function to be applied.

``````#Create a List
list.a <- list(c(1,1),c(3,3,1,4),c(4,5,6,7))
#Print a List
list.a

#Apply mean function
sapply(list.a,mean)

#Apply range function
sapply(list.a,range)``````

Output:

``````> list.a
[[1]]
[1] 1 1

[[2]]
[1] 3 3 1 4

[[3]]
[1] 4 5 6 7

> #Apply mean function
> sapply(list.a,mean)
[1] 1.00 2.75 5.50
>
> #Apply range function
> sapply(list.a,range)
[,1] [,2] [,3]
[1,]    1    1    4
[2,]    1    4    7``````

In above output, you can see when the mean function is applied, the output in form of a vector, and when the range function is applied in the output in the form of a matrix.

`mapply` is a multivariate function of `sapply`. This is multivariate in the sense that your function can accept multiple arguments.

`mapply(FUN,MoreArgs,..)`:

• `FUN` is function to be applied.

• `MoreArgs` means you can pass multiple arguments.

``````#Create two list
l1<-list(a=c(1:10),b=c(11:20),c=c(21:30))
l2<-list(a=c(1:10),b=c(11:20),c=c(21:30))

#Pring l1 and l2
l1
l2

#Apply sum function
mapply(sum,l1\$a,l1\$c,l2\$a,l2\$b)``````

Output:

``````> l1<-list(a=c(1:10),b=c(11:20),c=c(21:30))
> l2<-list(a=c(1:10),b=c(11:20),c=c(21:30))
> l1
\$a
[1]  1  2  3  4  5  6  7  8  9 10

\$b
[1] 11 12 13 14 15 16 17 18 19 20

\$c
[1] 21 22 23 24 25 26 27 28 29 30

> l2
\$a
[1]  1  2  3  4  5  6  7  8  9 10

\$b
[1] 11 12 13 14 15 16 17 18 19 20

\$c
[1] 21 22 23 24 25 26 27 28 29 30

> mapply(sum,l1\$a,l1\$c,l2\$a,l2\$b)
[1] 34 38 42 46 50 54 58 62 66 70``````

`rapply` is the recursive version of `lapply`. It applies functions to lists in different ways, depending on the arguments supplied.

`rapply(X,FUN,how,...)`:

• `X` is a list.

• `FUN` is a function to be applied.

• `how` means in which format the output is required.

``````#Create List
l1<-list(a=1:10,b=11:20)

#Print list
l1

#Apply sum function without how
rapply(l1,sum)

#Apply sum function with how
rapply(l1,sum,how="list")``````

Output:

``````> l1<-list(a=1:10,b=11:20)
> l1
\$a
[1]  1  2  3  4  5  6  7  8  9 10

\$b
[1] 11 12 13 14 15 16 17 18 19 20

> rapply(l1,sum)
a   b
55 155
> rapply(l1,sum,how="list")
\$a
[1] 55

\$b
[1] 155``````

`vapply` is similar to `sapply`, but has a pre-specified return value, so it is safer and sometimes faster to use.

`vapply(X,FUN,defaultvalues,...)`:

• `X` is a list.

• `FUN` is a function to be applied.

• `defaultvalues` needs to be specified.
``````#Create List
list1 <- list(a = 1:10, b = 11:20)

#Print List
list1

#fivenum of values using vapply
list.fivenum <- vapply(list1, fivenum, c(Min.=0, "1st Qu."=0, Median=0, "3rd Qu."=0, Max.=0))

#Print fivenum list
list.fivenum``````

Output:

``````> list1 <- list(a = 1:10, b = 11:20)
> list1
\$a
[1]  1  2  3  4  5  6  7  8  9 10

\$b
[1] 11 12 13 14 15 16 17 18 19 20

> list.fivenum <- vapply(list1, fivenum, c(Min.=0, "1st Qu."=0, Median=0, "3rd Qu."=0, Max.=0))
> list.fivenum
a    b
Min.     1.0 11.0
1st Qu.  3.0 13.0
Median   5.5 15.5
3rd Qu.  8.0 18.0
Max.    10.0 20.0``````

Now, you know the different types of apply functions with R!

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:
r ,data science ,big data ,apply function

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.