Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
The lapply() function
Other apply() functions
8. The sapply() function
The split-apply-combine pattern
Summary

Instruction

Nice! Let's go a little bit further and look at another function, sapply().

The sapply() functions works just like lapply(), except instead of returning a list, it tries to return the most simple data structure that can be used to represent the data. In fact:

sapply(x) = lapply(x, ..., simplify=TRUE)

For example, lapply(sales, sum) returns the total number of bracelets sold each day in a list. With sapply(sales, sum), R returns a vector. The results are the same, and the only thing that differs is how the results are represented:

> sapply(sales, sum)
monday tuesday wednesday thursday friday saturday sunday
3581 2684 2820 2162 2364 2443 3115

Exercise

Calculate the average number of bracelets sold each day. Use sapply().