• Post author:
  • Post category:R Programming
  • Post last modified:August 18, 2024
  • Reading time:11 mins read
You are currently viewing Explain sapply() Function in R

The sapply() function in R is used to apply a function to each element of a list, vector, or data frame and return the results as a vector or matrix. It belongs to the apply family of functions and is especially useful for applying a function over a list, vector, or data frame and returning a vector or matrix. The key difference between sapply() and <a href="https://sparkbyexamples.com/r-programming/explain-lapply-function-in-r/">lapply()</a> is that sapply() returns a vector or matrix, while lapply() always returns a list, regardless of the input.

Advertisements

In this article, I will explain the sapply() function, including its syntax, parameters, and usage, to demonstrate how it can be used to apply a function to each element of a given object and return a vector or matrix.

Key points-

  • sapply() applies a specified function to each element of a list, vector, or data frame and simplifies the result to a vector or matrix if possible.
  • Unlike lapply(), which always returns a list, sapply() returns a simplified result such as a vector or matrix, making it more convenient for certain tasks.
  • The simplify parameter controls whether the result is simplified to a vector or matrix. The default is TRUE, but it can be set to FALSE to prevent simplification.
  • By default, sapply() preserves the names of the input object in the output. This behavior can be controlled with the USE.NAMES parameter.
  • Additional arguments can be passed to the function being applied via the ... parameter, allowing for flexible and dynamic function application.
  • If simplify is TRUE, sapply() returns a vector, matrix, or higher-dimensional array, depending on the output of the applied function. If simplify is FALSE, it returns a list.
  • sapply() can be used with custom functions defined by the user, enabling tailored and specific operations on each element of the input object.
  • sapply() can handle vectors, lists, and data frames, making it a versatile function for various types of data structures in R.
  • sapply() can be more efficient and concise than using explicit loops for applying functions to elements of a data structure, contributing to cleaner and more readable code.

R sapply() Function

The sapply() function in R applies a specified function to each element of a list, vector, or data frame. By default, it simplifies the result to a vector or matrix. If you set the simplify parameter to FALSE, it returns the result as a list.

Syntax of sapply() Function

Following is the syntax of the sapply() function.


# Synatx of sapply() function
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

Parameters

  • X: A vector or list.
  • FUN: The function to apply to each element of X
  • ...: Additional arguments to pass to FUN.
  • simplify: Logical or character string; if TRUE, the result will be simplified to a vector or matrix if possible. The default is TRUE.
  • USE.NAMES:Logical; should the result preserve the names of X.

Return Value

  • If simplify = TRUE, the function returns a vector, matrix, or higher-dimensional array, depending on the output of FUN.
  • If simplify = FALSE, the function returns a list.

Apply sapply() Function to Vector

Let’s create a numeric vector and use the sapply() function to apply a specified function to each element in the vector. This function processes each element according to the specified function and returns the resulting vector.


# Use sapply() function to vector
# Create numeric vector
vec <- 1:5
print("Given vector:")
print(vec)
print("Get the type of given vector:")
print(class(vec))
result <- sapply(vec, function(x) x^2)
print("After applying sapply function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.

r sapply

Apply R sapply() Function to Data Frame

You can use the sapply() function on a data frame to apply a function to each column and return the result as a matrix. Let’s create an R data frame and pass it into this function along with a specified function to simplify the results into a matrix.


# # Use sapply() function to vector
# create the data frame
df <- data.frame(name = c(1, 2, 3, 4, 5))
print("Given data frame:")
print(df)

result <- sapply(df, function(x) x^2)
print("After applying sapply function to data frame:")
print(result)
print("Get the type of result:")
print(class(result))


Yields below output.

r sapply

Apply R sapply() Function to Listthis

You can also use the sapply() function on a list to apply a specified function to each element. First, define the list and pass it into sapply() along with the specified function. This will apply the function to each element of the list and return the result as a vector.


# Use sapply() function to list
# Create a list
list <- list(Course1 = 'r', Course2 = 'Python', Course2 = 'Spark')
print("Given list:")
print(list)
result <- sapply(list, toupper)
print("After applying sapply function to data frame:")
print(result)
print("Get the type of result:")
print(class(result))

# Output:
# [1] "Given list:"
# $Course1
# [1] "r"

# $Course2
# [1] "Python"

# $Course2
# [1] "Spark"

# [1] "After applying sapply function to data frame:"
# Course1  Course2  Course2 
#      "R" "PYTHON"  "SPARK" 

# [1] "Get the type of result:"
# [1] "character"

Use sapply() to Apply the Custom Function

Alternatively, you can use a user-defined function within the sapply() function to process R objects such as lists, vectors, or data frames. To do this, first define the custom function, then pass it into sapply() along with the list. This will apply the user-defined function to each element of the list and return the result as a vector.


# Apply custom function to vector
# Define the custom function 
cus_fun <- function(x) {
  return(x ^ 2)
}
# Create a vector of numbers
vec <- c(1, 2, 3, 4, 5)
result <- sapply(vec, cus_fun)
print("After applying sapply function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

# Output:
# [1] "After applying sapply function to vector:"
# [1]  1  4  9 16 25
# [1] "Get the type of result:"
# [1] "numeric"

Prevent the simplification

By default, sapply() simplifies the result to a vector or matrix. By setting the simplify argument to FALSE, you can prevent this simplification, causing it to return a list instead.


# Prevent the simplefication using
# Use sapply() function to vector
# Create numeric vecto
vec <- 1:5
result <- sapply(vec, function(x) x^2, simplify = FALSE)
print("After applying sapply function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

# Output: 
# [1] "After applying sapply function to vector:"

# [[1]]
# [1] 1

# [[2]]
# [1] 4

# [[3]]
# [1] 9

# [[4]]
# [1] 16

# [[5]]
# [1] 25

# [1] "Get the type of result:"
# [1] "list"

If X has names, sapply() will use these names in the output by default. You can control this by using the unname() function to remove the names from the result.


# Use sapply() function and unname() function to 
# remove the names of values
# Create a list
list <- list(Course1 = 'r', Course2 = 'Python', Course2 = 'Spark')
result <- unname(sapply(list, toupper))
print("After applying sapply function to data frame:")
print(result)
print("Get the type of result:")
print(class(result))

# Output:
# [1] "After applying sapply function to data frame:"
# [1] "R"      "PYTHON" "SPARK" 
# [1] "Get the type of result:"
# [1] "character"

Conclusion

In this article, I have explained that the sapply() function is a versatile tool in R for applying functions to elements of a vector, list, or data frame and returning a simplified result. Unlike lapply(), which always returns a list, sapply() attempts to simplify the output to a vector or matrix, making it especially useful when a simplified structure is needed. By understanding its syntax, parameters, and behavior, you can effectively use sapply() to streamline your data processing tasks in R.

Happly learning !!