Do more with tidyverse
Closing this chapter, we will quickly study a few functions from tidyverse that were not mentioned in any of the previous sections but that can be very helpful when solving data wrangling problems.
Consider again the mtcars
dataset (load it with data('mtcars')
), which has information about 32 cars from the 1974 Motor Trend Use magazine. We are already familiar with that data, and we can use it as a reference to learn about the next few transformations.
Let’s dive right in on a couple of functions of the purrr
library. This library brings functions like those from the Apply family, studied in Chapter 5. The most interesting function to look at is the map()
function. It applies the same function to every element of a vector or list. If we want to map the average of the variables’ horsepower and weight, this is how to do it:
# Map mtcars %>% select(hp, wt) %>% map(mean) $hp [1] 146.6875 $wt [1] 3.21725...