1. Learn
  2. /
  3. Courses
  4. /
  5. Foundations of Functional Programming with purrr

Exercise

Simulating data with multiple inputs using map2()

The map() function is great if you need to iterate over one list, however, you will often need to iterate over two lists at the same time. This is where map2() comes in. While map() takes the list as the .x argument; map2() takes two lists as two arguments: .x and .y.

To test out map2(), you are going to create a simple dataset, with one list of numbers and one list of strings. You will put these two lists together and create some simulated data.

Instructions

100 XP
  • Create a means list containing the values 1 through 3, each as a separate element.
  • Create a sites list with "north", "west", and "east".
  • map2() over the sites and means lists to create a data frame with two columns.
    • First column is sites; second column is generated by rnorm() with mean from the means list.