Apply functions in R

Apply functions are alternatives to Loops in R. In this tutorial, we are going to discuss about apply and apply family functions.

We are going to explain following apply functions with examples:

apply()
sapply()
lapply()
mapply()
vapply()
rep()
rapply()
tapply()

apply Function:

Definition: Returns a vector or array or list of values obtained by applying a function to margins of an array or matrix.

Syntax of apply:

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

Where
X is An Matrix
MARGIN is a variable defining how the function is applied: when MARGIN=1, it applies over rows, whereas with MARGIN=2, it works over columns. Note that when you use the construct MARGIN=c(1,2), it applies to both rows and columns
FUN is the function to be applied

Examples for apply function:

Create matrix ‘a’

> a< -matrix(c(1:15),nrow=3) > a [,1] [,2] [,3] [,4] [,5] [1,] 1 4 7 10 13 [2,] 2 5 8 11 14 [3,] 3 6 9 12 15

Calculate mean of each column of matrix a

> apply(a,MARGIN=2,mean) [1] 2 5 8 11 14

Calculate mean of each row of matrix a

> apply(a,MARGIN=1,mean) [1] 7 8 9

Calculate sum of each column of matrix a

> apply(a,2,sum) [1] 6 15 24 33 42

Calculate sum of each row of matrix a

> apply(a,1,sum) [1] 35 40 45

lapply Function:
returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.

Syntax of lapply:

lapply(X, FUN, ...)

Where
X is vector(list) or an object.
FUN is the function to be applied to each element of X.

Examples for lapply():

theList< -list(A=matrix(1:9,nrow=3),B=1:5,C=matrix(1:4,nrow=2),D=5)

> theList $A [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 $B [1] 1 2 3 4 5 $C [,1] [,2] [1,] 1 3 [2,] 2 4 $D [1] 5

Calculate sum of each element of List

> lapply(theList,sum) $A [1] 45 $B [1] 15 $C [1] 10 $D [1] 5

Observeration: Output of lapply function is list.

sapply Function:

Definition: sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix

Syntax of sapply:

sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

Where
X is vector(list) or an object.
FUN is the function to be applied to each element of X.

theList< -list(A=matrix(1:9,nrow=3),B=1:5,C=matrix(1:4,nrow=2),D=5)

> theList $A [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 $B [1] 1 2 3 4 5 $C [,1] [,2] [1,] 1 3 [2,] 2 4 $D [1] 5

Calculate sum of each element of List

> sapply(theList,sum) A B C D 45 15 10 5

Observation: Output of lapply function is vector.

mapply function:

Definition: mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each … argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

Syntax of mapply() function:

mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)

Examples of mapply function:

1. Compare two vectors using mapply function:

A< -c(1:10) B<-c(1:5)

> mapply(identical,A,B) [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE

2. Compare two lists using mapply function firstList< -list(A=matrix(1:16,4),B=matrix(1:16,2),c=1:5)

> firstList $A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 $B [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 3 5 7 9 11 13 15 [2,] 2 4 6 8 10 12 14 16 $c [1] 1 2 3 4 5

secondList< -list(A=matrix(1:16,4),B=matrix(1:16,8),c=15:1)

> secondList $A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 $B [,1] [,2] [1,] 1 9 [2,] 2 10 [3,] 3 11 [4,] 4 12 [5,] 5 13 [6,] 6 14 [7,] 7 15 [8,] 8 16 $c [1] 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Compare elements of above lists are same or not.

mapply(identical,firstList,secondList) A B c TRUE FALSE FALSE

vapply function:

Definition: vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use.

Syntax of vapply:

vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

Where
X is vector(list) or an object.
FUN is the function to be applied to each element of X.

Examples for vapply():

vapply(theList,sum,FUN.VALUE = 0) A B C D 45 15 10 5

rapply function:

Definition: rapply is a recursive version of lapply.

Syntax of rapply function:

rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list"), ...)

Where
Object is list
f is function of single argument

Examples of rapply function:

Declare List

A < - list(list(a = pi, b = list(c = 1:2)), d = "a test")

> rapply(A, sqrt, classes = “numeric”, how = “replace”) [[1]] [[1]]$a [1] 1.772454 [[1]]$b [[1]]$b$c [1] 1 2 $d [1] “a test”

tapply function: Definition: Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors. Syntax for tapply function:

tapply(X, INDEX, FUN = NULL, ..., simplify = TRUE)

Where
X is an object
INDEX is list of one or more factors, each of same length as X. The elements are coerced to factors by as.factor.
FUN is the function to be applied

Examples of tapply function:

> n < - 17 > fac < - factor(rep(1:3, length = n), levels = 1:5) > fac [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 Levels: 1 2 3 4 5

> tapply(1:n, fac, sum) 1 2 3 4 5 51 57 45 NA NA

> tapply(1:n, fac, range) $`1` [1] 1 16 $`2` [1] 2 17 $`3` [1] 3 15 $`4` NULL $`5` NULL

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools
%d bloggers like this: