The Apply Family of Functions
If one has to debate on a few powerful features of R programming, the apply family of functions, would find a mention. It is used commonly to avoid using looping structures such as for and while even though they are available in R.
First, it's slow to run for loops in R and second, the implementation of the apply functions in R is based on efficient programming languages such as C/C++, which makes it extremely fast to loop.
There are many functions in the apply family. Depending on the structure of the input and output required, we select the appropriate function:
apply()
lapply()
sapply()
vapply()
mapply()
rapply()
tapply()
We will discuss a few in this section.
The apply Function
The apply() function takes an array, including a matrix, as input and returns a vector, array, or list of values obtained by applying a function to margins of an array or matrix.
Exercise 13: Implementing the apply Function
In this exercise, we will count the number of vowels in each column of a 100 x 100 matrix of random letters from the English alphabet. The MARGIN = 1 function will scan each row, and MARGIN = 2 will specify the column. The same function will the count vowels in each row.
Perform the following steps to complete the exercise:
Create a 100 x 100 matrix of random letters (ncol is the number of columns and nrow is the number of rows) using the following command:
r_characters <- matrix(sample(LETTERS, 10000, replace = TRUE), ncol = 100, nrow = 100)
Now, create a function named c_vowel to count the number of vowels in a given array:
c_vowel <- function(x_char){ return(sum(x_char %in% c("A","I","O","U"))) }
Next, use the apply function to run through each column of the matrix, and use the c_vowel function as illustrated here:
apply(r_characters, MARGIN = 2, c_vowel)
The output is as follows:
## [1] 17 16 10 11 12 25 16 14 14 12 20 13 16 14 14 20 10 12 11 16 10 20 15 ## [24] 10 14 13 17 14 14 13 15 19 18 21 15 13 19 21 24 18 13 20 15 15 15 19 ## [47] 13 6 18 11 16 16 11 13 20 14 12 17 11 14 14 16 13 11 23 14 17 14 22 ## [70] 11 18 10 18 21 19 14 18 12 13 15 16 10 15 19 14 13 16 15 12 12 14 10 ## [93] 16 16 20 16 13 22 15 15
The lapply Function
The lapply function looks similar to apply(), with a difference that it takes input as a list and returns a list as output. After rewriting our previous example in the following exercise, the output of class function shows that the output is a list.
Exercise 14: Implementing the lapply Function
In this exercise, we will take a list of vectors and count the number of vowels.
Perform the following steps to complete the exercise:
Create a list with two vector of random letters, each of size 100:
r_characters <- list(a=sample(LETTERS, 100, replace = TRUE), b=sample(LETTERS, 100, replace = TRUE))
Use the lapply function to run through on list a and b, and the c_vowel function to count the number of vowels from the list:
lapply(r_characters, c_vowel)
The output is as follows:
## $a ## [1] 19 ## $b ## [1] 10
Check the class (type) of the output. The class() function provides the type of data structure:
out_list <- lapply(r_characters, c_vowel) class(out_list)
The output is as follows:
## [1] "list"
The sapply Function
The sapply function is just a wrapper on the lapply function, where the output is a vector or matrix instead of a list. In the following code, observe the type of the output after applying sapply difference. The output returns a vector of integers, as we can check with the class() function:
sapply(r_characters, c_vowel) ## a b ## 19 10
To print the class of the output, use the following command:
out_vector <- sapply(r_characters, c_vowel) class(out_vector)
The output of the previous command is as follows:
## [1] "integer"
The tapply Function
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. The tapply function is quite useful when it comes to working on a subset level of data. For example, in our aggregate function, if we were to get an aggregate like standard deviation for the type of Iris species, we could use tapply. The following code shows how to use the tapply function:
First, calculate the standard deviation of sepal length for each Iris species:
tapply(iris$Sepal.Length, iris$Species,sd)
The output is as follows:
## setosa versicolor virginica ## 0.3524897 0.5161711 0.6358796
Next, calculate the standard deviation of sepal width for each of the Iris species:
tapply(iris$Sepal.Width, iris$Species,sd)
The output of the previous command is as follows:
## setosa versicolor virginica ## 0.3790644 0.3137983 0.3224966
Now, let's explore some popular and useful R packages that might be of value while building complex data processing methods, machine learning models, or data visualization.