BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Foundations of Functional Programming with purrr

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • 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.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# List of 1, 2 and 3
means <- list(___)

# Create sites list
sites <- list(___)

# Map over two arguments: sites and means
list_of_files_map2 <- map2(___, ___, ~___(sites = ___,
                           a = rnorm(mean = ___, n = 200, sd = (5/2))))

list_of_files_map2
Kodu Düzenle ve Çalıştır