# Example list
<- list(a = 1:5, b = 6:10, c = 11:15)
my_list
# Applying a function to each element of the list using lapply
<- lapply(my_list, sum)
result
# Print the result
print(result)
$a
[1] 15
$b
[1] 40
$c
[1] 65
Steven P. Sanderson II, MPH
August 21, 2024
Explore the differences between lapply() and sapply() in R with practical examples. Learn when to use each function and how they simplify data operations.
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!
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:
my_list
containing three elements: vectors of numbers.lapply()
, we applied the sum()
function to each element in the list.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.
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:
sapply()
instead of lapply()
.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.
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.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.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:
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.
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!