I've used purrr
in R to simulate data (with B
iterations) and run models in order to evaluate the performance of three approaches. I want to collect the results into a list of three tibbles (each with B
rows) on which to perform analysis of the methods. How can I use functional programming principles in R (purrr
) to achieve this. Here's an example:
Take this function that creates a list of length = r
, with each of r
elements consisting of n
draws from a standard normal:
list_norms <- function(n, r, seed) {
set.seed(seed)
map(1:r, rnorm, n = n) %>%
set_names(c("A", "B", "C"))
}
Then I use map to simulate 10 times:
map(1:10, list_norms, n = 5, r = 3)
The result here is a list of length 10, where each element is a list of length 3 (named A, B, and C), where each element therein is a vector of 5 draws from a normal distribution. I want to end up with a list of length 3, one for each of A, B, & C, each containing a tibble with ten rows (one for each iteration of the simulation) and 5 columns (one for each draw from the normal).
Is there a way to achieve this with functional programming principles in R, using purrr
or other libraries in the tidyverse? I'm looking at some combination of map & reduce.