Using the apply functions in R
One of the core functionalities of R is the apply family of functions, which are designed to simplify the manipulation of data in R. The apply functions allow us to perform a single operation over multiple elements of an array or data frame, and return the result in a specific format. In this blog post, we will explore the different apply functions in R and their use cases and examples.
1. lapply
lapply() stands for “list apply” and is used to apply a function to each element of a list and return a list. The syntax for lapply is as follows:
lapply(X, FUN, ...)
where X is the list on which the function will be applied, and FUN is the function that will be applied to each element of the list. The output will be a list with the same length as the input list.
Example: Suppose we have a list of numbers and we want to calculate their squares using the lapply function. Here is how we can do it:
> my_list <- list(1, 2, 3, 4, 5)
> squared_list <- lapply(my_list, function(x) x^2)
> squared_list
[[1]]
[1] 1
[[2]]
[1] 4
[[3]]
[1] 9
[[4]]
[1] 16
[[5]]
[1] 25
2. sapply
sapply() is similar to lapply(), but it simplifies the output to the most appropriate data type. The syntax for sapply is as follows:
sapply(X, FUN, …)
where X is the list on which the function will be applied, and FUN is the function that will be applied to each element of the list. The output will be a vector or matrix, depending on the input and output of the function.
Example: Suppose we have a list of numbers and we want to calculate their squares using the sapply function. Here is how we can do it:
> my_list <- list(1, 2, 3, 4, 5)
> squared_list <- sapply(my_list, function(x) x^2)
> squared_list
[1] 1 4 9 16 25
3. vapply
vapply() is similar to sapply(), but it allows you to specify the output data type explicitly. The syntax for vapply is as follows:
vapply(X, FUN, FUN.VALUE, ...)
where X is the list on which the function will be applied, FUN is the function that will be applied to each element of the list, and FUN.VALUE is the output data type that the function should return. The output will be a vector or matrix of the specified data type.
Example: Suppose we have a list of numbers and we want to calculate their squares using the vapply function, and we want the output to be of type numeric. Here is how we can do it:
> my_list <- list(1, 2, 3, 4, 5)
> squared_list <- vapply(my_list, function(x) x^2, FUN.VALUE = numeric(1))
> squared_list
[1] 1 4 9 16 25
4. tapply
Example: Suppose we have a data frame that contains information on students in a school, including their grades and the subject they took the exam in.
name subject grade
1 Alice Math 90
2 Bob English 80
3 Charlie Math 95
4 David Science 85
5 Eve English 75
We can use tapply()
to calculate the mean grade for each subject:
> tapply(students$grade, students$subject, mean)
English Math Science
77.5 92.5 85.0
5. mapply
The mapply() function applies a function to multiple lists in parallel. The function takes the first element from each list and applies the function to them, then it takes the second element from each list and so on until it reaches the end of the shortest list.
Example: Here’s an example of how mapply() works:
a <- c(1, 2, 3)
b <- c(4, 5, 6)
c <- c(7, 8, 9)
mapply(function(x, y, z) x * y + z, a, b, c)
[1] 11 18 27
In this example, the function takes three arguments: x, y, and z. The mapply() function applies the function to the first elements of a, b, and c, so x = 1, y = 4, and z = 7. The result of the function is 11. Then it applies the function to the second elements of a, b, and c, so x = 2, y = 5, and z = 8. The result of the function is 18. Finally, it applies the function to the third elements of a, b, and c, so x = 3, y = 6, and z = 9. The result of the function is 27.
Happy l-s-t-v-m-applying!!!