I want to use tibble, df, a list of scenarios, myscenarios, and a simulation function ,simu, to create a result tibble with:
- 25 rows of data (five rows for each scenario)
- The result data frame should include the following columns: x, n, v, scenario, result
I would like to achieve this using the appropriate purrr function.
The reprex below provides the tibble, a list of five scenarios and the simu function. The current output simply utilizes the simu function against the df tibble.
Would lmap be the correct purrr function to achieve this? If so, would I have to use lmap in conjuction with mutate?
library(tidyverse)
df <- tibble(x= 1:5,
n= c("Jim","John","Jane","Jay","Jack"),
v= 11:15)
myscenarios <- list("one", "two", "three", "four", "five")
simu <- function(x, v){
x * v + sample(1:10, 1)
}
result <- df %>%
mutate(result = simu(x, v))
result
#> # A tibble: 5 x 4
#> x n v result
#> <int> <chr> <int> <int>
#> 1 1 Jim 11 21
#> 2 2 John 12 34
#> 3 3 Jane 13 49
#> 4 4 Jay 14 66
#> 5 5 Jack 15 85
Created on 2020-11-23 by the reprex package (v0.3.0)
myscenarios
used in the function – akrun