End of Summer - hours only!Up to 80% off on all courses and bundles.-Close
The lapply() function
Other apply() functions
10. Anonymous functions with sapply()
The split-apply-combine pattern


The result look like those given by lapply(), right? When simplify is set to FALSE, sapply() works exactly like lapply() – it returns a list. The sapply() function will also work with anonymous functions.


Let's practice with one more exercise. Calculate the ratio between the least and most number of bracelets sold on each day:

\text{ratio} = \frac{min(\text{number\ of\ sold\ bracelets})}{max(\text{number\ of\ sold\ bracelets})}

This will tell us if we have lots of sales variation – if the ratio is close to 1, then the minimum and maximum are really close, which means the range of values is more closely packed. If the ratio is close to 0, then that means the range of sales differed greatly on that particular day.

Use sapply() with two arguments: sales and an anonymous function that calculates the ratio described above.

Stuck? Here's a hint!


sapply(sales, function(x){