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

Instruction

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.

Exercise

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!

Type:

sapply(sales, function(x){
  return(min(x)/max(x))
})