R Tutorial : The many flavors of map()

Learn the map variants in R's purrr package: mapdbl() returns numeric vectors, maplgl() returns logical vectors, and mapchr() returns character vectors. The tutorial uses a list of data frames to extract row counts and site names, then builds a new data frame for quality control checks.
Chapters
If you remember from the previous lessons, the map function outputs a list. Sometimes we want our data in another class, which is where the other flavors of map come in, giving us many different kinds of outputs. In the last lesson we saw that map outputs a list. Sometimes this can be quite useful. But if we think about the kinds of problems we solve with data every day, we may want a different kind of output, like a vector. This is where the other flavors of map come in. Here is the example from the previous lesson where the map function gives us the number of rows in each data frame from the surveydata list. Pulling out a vector of numbers from a list can be useful for a number of things, including as an input for our next task or to create a new column in a dataframe. Here we are using another map function, mapdbl, to pull out the numbers that represent the number of rows in each dataframe as a vector instead of a list. The regular map function gives us a list, while mapdbl outputs a vector of numbers as the result. The vector of true and false values can be valuable for checking to make sure an object contains what we think it does, or as an input to something else. Here we are interested in checking whether each element of our SurveyData list has 14 rows. So we use the maplgl function to get a logical vector as an output. A vector of characters can be valuable as a way of pulling information out of a list, so it can be used for other things, often as an input for our next tasks. Here we want to pull out the names of the amphibians surveyed as a character vector with the site names. We do this with mapchr. Here we want to create a vector that contains the number of rows in each element of the surveydata list and make it a column in a new dataframe. First we create our new data frame. We'll call it surveyrows. This new data frame has two columns, one called Names, which contains the names of each element from the surveydata A list. The second column is called Rows and is empty for now. Next, we'll use MapDBL to create a vector of numbers. Our first argument is the SurveyData list. Our second argument is the tilde symbol followed by the NROW function. Inside nrow, we put X to represent where each element of the surveydata list should be placed. We then assign mapdbl to the rows column in a new dataframe that we created. Now we can use these names and rows to determine if these elements contain the number of rows expected, which is a vital part of the quality assurance and quality control process. Unlike our example from the previous video, here we have a nice vector that easily works as a part of a dataframe column, whereas before the output was a list of values which can be more challenging to work with. Now it's your turn.
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







