R Tutorial: The map family of functions

Learn how map functions iterate over list elements and apply functions to nested data. Use map_dbl to return vectors directly, or map with lm to fit separate linear models for each country's data.
Chapters
In the last video and exercise series, you learned how to use the NEST and UNNEST functions for steps 1 and 3 of the list column workflow. In this lesson, I will introduce you to the MAP family of functions. These functions fulfill the roles of steps two and three of this workflow. The map function applies a desired function to to every element in a vector or a list and always returns a list as its result. This function requires two parameters, X and F. X is the vector or list that you want to iterate over, while F is the function. The function can either be a predefined function or it can be an anonymous function using the formula syntax. For example, if you wanted to use the mean function, you can refer to it directly, or you can build an anonymous function using the tilde to indicate that you are using a formula and the x to indicate the value placeholder. In the previous exercise, you calculated the mean population of the country of Algeria by extracting the first element of of the nested data frame, then calculating the mean of the population column. The structure of this is very similar. When using Map, you will use MAP to calculate the population mean for each country using the corresponding nested data frame of that country. Here, the X parameter is the data column in the nested data frame. Remember that this column is a list of data frames corresponding to each country. Since these are data frames, you need to use an anonymous function to explicitly calculate the mean for the population column of each data frame. Remember that the X here acts as the placeholder for each element of the list, since you know that this list contains data frames and you want to calculate the mean of the population column for from each data frame, you can refer to the placeholder the same way you would working with a single element. The result of this function is a list of population means for the 77 countries. Remember that tables are special data frames that allow us to store arbitrarily complex list columns. Because of this, you can append the resulting list of population means using the mutate function. Of course, storing a list of doubles isn't very practical for exploration, so you need to simplify these columns using unnest. Let's revisit these steps in the context of the list column workflow. First, we made a list column of data frames for each country using Nest. Then we worked with the list columns by calculating the population mean of each data frame using using Map. Finally, we simplified the resulting nested column with the UNNEST function. In certain situations, you can combine the last two steps using another function from the map family. If you know that the output of the map function is a vector of a specific type, you can use a map function corresponding to that type to calculate the result and and explicitly return a vector of the expected type. For example, the mean function returns a vector of type double. As such, you can use mapdouble to return a vector of doubles instead of a list of doubles. This can be done like so, and as a result, mutate appends a vector of type double to the data frame instead of a list. You can also use map to build models for each country. Here the LM function is used to build linear models to predict the population using the fertility feature. You can define the model using the formula parameter and provide the data for each model using the X approach to refer to each country's dataframe when mapping. So let's map some data.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







