Get startedGet started for free

More map()

1. More map()

In this section we will cover some additional tasks you can do with map(), like simulating data, running linear models, and outputting different classes of information.

2. Simulate data

Often to test an idea we want to simulate data. We might want to do this because we need to do some analyses to see what sample size is needed to answer a question in an experiment. map() makes simulating multiple datasets simple. Here we have a list with four elements, each contains the value we want to use as the mean for a simulated dataset. To simulate our new datasets, we will be using our list of means as our first argument in map(), and then for our second argument, we take the values from that element, represented by dot x, and put it into the rnorm() function, after the mean argument. We also have to fill in the regular arguments, in this case, sd, the standard deviation, and n, the sample size.

3. Run linear models

We can also use `map()` to run the same model with different inputs, where each different input is one element from a list. Here we are taking the list object, using map() to run a linear model, and then summarizing the results of each model. Later on, we'll learn map2(), which will allow you to run different models as well.

4. map_*() flavors

There are lots of flavors of map(), we mentioned a few in Chapter 1. Let's now take a closer look. If an element in a list is a character, we can use map_chr() to transform all those elements into a vector of character strings, or we can use map_lgl() to ask TRUE FALSE questions and get a logical vector output.

5. map_*() flavors with numbers

If we want to output a vector of numbers, we have two choices. If the numbers are integers, whole numbers, you can use map underscore dbl, or map underscore int, and get the same output. Here we are working with some bird measurement data, one bird is a robin, another a sora. For each bird, we have its weight in grams and its wing length in centimeters. If the numbers do not have decimals, like wing length, then map_dbl() and map_int() return the same answer. If the numbers do have decimals, like weight, then map_dbl() will work fine, but map_int() will throw an error.

6. map_df()

The different map_*() functions we have covered thus far output vectors, which are one-dimensional data. Often times though we want a dataframe, which is two dimensional, made up of rows and columns. Dataframes have great utility and are required input by many functions. Here we want to take our list object, bird_measurements, and make a dataframe with the columns, weights, and wing lengths so we can graph it. We can do this with the map_df() function. First we pipe bird_measurements into map_df(). We put the data_frame() function after the tilde symbol. Inside data_frame() we set weight equal to dot x double bracket weight close double bracket. This means that each element of the weight element of each list element will be a row in the dataframe. Then we set wing underscore length equal to dot x double bracket wing length close double bracket. This returns a 2 by 2 tibble, which is a special kind of dataframe, with a weight column, and a wing length column.

7. Let's purrr-actice!

Now it's your turn to get familiar with all the map_* flavors.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.