Working With Functionals
In the Working With Vectorized Functions lesson, you learned to work with vectorized R functions to perform operations on all elements of a vector simultaneously.
But not all functions in R can be applied to all elements in a vector at once — some must be executed row by row. While you could use a for loop to do this, it isn't very easy to see what's going on.
As an alternative to for loops, you can use R's functionals. Functionals are a special kind of function. They take a function and a list or vector of data, apply it as inputs, and return a list or vector as output. Functionals eliminate the need for `for` loops in many situations by allowing you to apply any function to all elements of a list or vector.
In this lesson, you'll learn to work with functionals to replace for loops in a number of data manipulation scenarios using the functions you wrote in the Writing Custom Functions as you analyzed FiveThirtyEight's data on the 2014 FIFA World Cup.
While base R includes a family of functions, you'll focus on teaching tools for using functionals from a relatively recent addition to the
purrr package. The functionals in the
purrr package can be used for the same purposes as the apply family of functionals, and their consistency in syntax and output makes them easier to use and allows for more legible code.
1. Introducing Functionals
2. Working With Functionals From the Tidyverse purrr Package
3. Using Functionals to Apply Custom Functions
4. Functionals to Return Vectors of Specified Types
5. Functionals for Two-Variable Functions
6. Functionals for Returning Vectors of Specific Types from Functions With Two Variables
7. Functionals for Functions with More Than Two Variable Arguments
8. Next Steps