1. sapply() - simplify it!
Next up in the family of apply functions is sapply, short for simplify apply. This function is really nice for interactive use, because it attempts to simplify your output from a list to something more functional like a vector or matrix.
2. sapply()
To see this, let's return once more to the stock_list example. The way to call sapply is exactly the same as with lapply. Remembering that lapply returned a list when we applied the class function, what will sapply return? A named vector! You can then instantly use that in further analysis, without having to extract the elements from the list.
3. Apply a custom summary function
Here's another example. simple_summary is a function that returns the mean and standard deviation of a stock's returns as a vector. stock_return is a data frame where the columns are returns for apple, ibm, and microsoft. If you apply simple_summary individually to all 3 of the stocks in stock_return, you'll get 3 vectors that each contain the mean and standard deviation of that stock. But when you use sapply, it sees this list of vectors and instead collapses it into a simplified matrix. Pretty neat!
4. Let's practice!
A word of warning, while sapply is great for use by itself, if you are trying to write robust functions that are predictable, don't use sapply. The reason for this is that its simplification feature is great, until it doesn't work the way you expect it to. In the exercises, you'll go through an example where sapply can't simplify, and you'll find out what it returns instead. Good luck!