What is sapply() Function in R

The sapply() function works like lapply(), but it tries to interpret the output to the most fundamental data structure possible, either Vector or Matrix. The sapply() is a wrapper” function for lapply().

The apply() function is bundled with R essential package if installed an R with Anaconda. The apply() function returns the vector or array by applying a function to the margins of the array or matrix.

In addition, the lapply() function helps perform operations on list objects and returns the list object of the same length as the original set.

Iterating over a Vector in R

To iterate over a Vector in R, use the for loop. To calculate the cube of all the vector elements in R, you iterate each element using the for loop and calculate each element’s cube and return it.

op <- numeric(5)

for (num in 1:5) {
 op[num] <- num^3
}
print(op)

Output

[1] 1  8  27  64  125

But with the sapply() function, you can write this code with just one line without for loop. Let’s see what is sapply() and how to use the sapply() function in R with different use cases.

sapply in R

The sapply in R is a built-in function that applies a function to all the input elements. The sapply() method takes a list, vector, or data frame as an argument and returns a vector or matrix. The sapply() is an R wrapper class to lapply, with the difference being it returns a vector or matrix instead of a list object.

Syntax

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

Parameters

X: It is a vector or List appropriate to a call to sapply.

FUN: It is a function.

…: It is the optional argument to FUN.

simplify: It is a logical value that should the result be simplified to a vector or matrix if possible.

USE.NAMES: It is a logical value; if TRUE and X are characters, use X as a name for the result unless it had names already.

How to Use sapply() function in R

To use the sapply() function in R, you have to define the List or Vector you want to iterate on the first parameter and the function you want to apply to each element of the vector in the second argument.

Implementation of  sapply() function

Let’s take the above example where we used for loop to calculate the cube of each vector element.

sapply(1:5, function(num) num ^ 3)

Output

[1] 1  8  27  64  125

You can see that just with one line of code, we got a cube of all the elements of the vector.

rv <- 1:10
rv2 <- 11:20

l <- list(a = rv, b = rv2) 
sapply(l, mean)

Output

  a    b
 5.5  15.5

Now, let’s use the lapply() function instead of sapply() function.

rv <- 1:10
rv2 <- 11:20

l <- list(a = rv, b = rv2)
lapply(l, mean)

Output

$a
[1] 5.5

$b
[1] 15.5

The sapply() function is more efficient than lapply() because sapply() stores values directly into a vector. If you apply the lapply() function, then it would give us a list unless you pass simplify=FALSE as a parameter to sapply(). Then, a list will be returned.

Using sapply() function to R Vector

You can apply the sapply() function to a Vector. It returns the processed output. For example, if we want the square of all vector elements, we use the sapply() function and pass the two arguments.

  1. A vector: It is the vector that contains elements
  2. A function: The logic of square means the function that returns the square of the elements.

See the following code on how to use the sapply() function on R Vector.

rv <- 19:21
sapply(rv, function(f) f ^ 2)

Output

[1] 361 400 441

Using sapply and lapply in R

The sapply() function returns the output in Vector or Matrix.

The lapply() function returns the output in List.

We can use the lapply() or sapply() function interchangeably to slice the data frame. 

Difference between apply(), sapply() and lapply()

Function Arguments Objective Parameters Return Value
apply() apply(obj, margin, fun) Apply a function to the rows or columns or both. Data frame or matrix vector, list, array
lapply() lapply(obj, fun) Apply a function to all the elements of the input object. List, vector, or data frame list
sapply() sapply(obj, fun) Apply a function to all the elements of the input object. List, vector, or data frame vector or matrix

Conclusion

The apply() function applies a function to the rows or columns or both in the data frame.

The lapply() function applies a function to all the elements of the input object.

The sapply() function applies a function to all the elements of an input object.

That is it for the sapply() function in R.

1 thought on “What is sapply() Function in R”

Leave a Comment