lapply vs. sapply in R: What’s the Difference?

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

lapply vs. sapply in R: What’s the Difference?

In the world of R programming, understanding the difference between lapply() and sapply() can make your coding life much easier. These two functions are part of R’s powerful apply family, which allows you to perform operations over a list or vector with ease. But when should you use lapply() and when is sapply() the better choice? Let’s explore!

What is lapply()?

The lapply() function in R applies a function to each element of a list (or vector) and returns a list. It’s a versatile tool, especially when you need to preserve the structure of your output as a list.

Here’s a quick example:

# Example list
my_list <- list(a = 1:5, b = 6:10, c = 11:15)

# Applying a function to each element of the list using lapply
result <- lapply(my_list, sum)

# Print the result
print(result)
$a
[1] 15

$b
[1] 40

$c
[1] 65

Explanation:

  • We created a list my_list containing three elements: vectors of numbers.
  • Using lapply(), we applied the sum() function to each element in the list.
  • The output, result, is a list where each element is the sum of the numbers in the original list.

This is what lapply() is all about: it gives you a list, no matter what.

What is sapply()?

On the other hand, sapply() is a simplified version of lapply(). It tries to simplify the result into a vector or matrix when possible, making your output more readable in certain situations.

Let’s look at the same example using sapply():

# Applying a function to each element of the list using sapply
result <- sapply(my_list, sum)

# Print the result
print(result)
 a  b  c 
15 40 65 

Explanation:

  • This time, we used sapply() instead of lapply().
  • The output is now a simple vector, where each element corresponds to the sum of the numbers in the original list.

Notice how sapply() simplifies the result into a vector? This is particularly useful when you want your output to be more concise and less complex.

Key Differences

  • Output Type: lapply() always returns a list, while sapply() attempts to return a vector or matrix if possible. If it can’t, it will fall back to returning a list.
  • Usage: Use lapply() when you need to maintain the structure of your output as a list. Choose sapply() when you prefer a simplified result, like a vector or matrix.

Practical Example: Mean Calculation

Let’s go through another example to see the differences more clearly:

# Example list of numeric vectors
data <- list(a = c(4, 6, 8), b = c(10, 15, 20), c = c(25, 30, 35))

# Using lapply to calculate the mean of each vector
mean_lapply <- lapply(data, mean)
print(mean_lapply)
$a
[1] 6

$b
[1] 15

$c
[1] 30
# Using sapply to calculate the mean of each vector
mean_sapply <- sapply(data, mean)
print(mean_sapply)
 a  b  c 
 6 15 30 

Explanation:

  • We have a list data with three numeric vectors.
  • lapply(data, mean) returns a list, where each element is the mean of the corresponding vector.
  • sapply(data, mean) returns a vector, simplifying the output.

This example clearly shows how lapply() and sapply() handle the output differently. If you need the output as a list, go for lapply(). If a vector suits your needs, sapply() is the better option.

Conclusion

Both lapply() and sapply() are handy functions in R that help you avoid writing loops. The choice between them depends on the output format you desire. lapply() will always give you a list, while sapply() tries to simplify the result.

Why not try out both functions with your own data? Experiment with different scenarios to see how each one behaves. And remember, the best way to master these tools is to practice!

I’d love to hear your thoughts on this topic. Have you encountered situations where one function worked better than the other? Drop your comments below, and let’s discuss!


Happy Coding!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)