library(tidyverse)
mean_sales <- c(dairy = 5364.5846, meat = 5059.6955, fish = 764.4324, deli = 1744.4206, cheese = 364.5226)
sd_sales <- c(dairy = 1192.3751, meat = 1560.7741, fish = 333.7008, deli = 509.8426, cheese = 127.2061)
# This is from the lesson---this is how you iterate over two things
map2(mean_sales, sd_sales, \(.x, .y) rnorm(30, .x, .y))
#> $dairy
#> [1] 5260.738 5948.625 4823.229 5332.571 5087.384 3951.635 3381.243 4245.053
#> [9] 5460.541 5270.912 5895.352 5896.304 4832.004 5012.681 5339.073 7458.869
#> [17] 6280.633 4781.151 5404.761 4731.484 6945.393 3285.835 5314.774 5749.308
#> [25] 6674.452 3881.163 6569.051 4827.528 5169.196 4821.271
#>
#> $meat
#> [1] 8644.167 3453.191 3271.079 4928.379 4124.268 1881.426 6480.195
#> [8] 3534.327 4596.639 5727.803 5891.607 5722.235 5486.429 2597.559
#> [15] 2274.264 4860.938 6744.098 6737.600 6681.131 5626.661 6150.697
#> [22] 2572.164 7138.863 5520.908 11026.545 4151.027 5209.156 7353.454
#> [29] 2696.729 4860.909
#>
#> $fish
#> [1] 1352.42153 487.62386 270.17063 479.27642 986.27394 861.33735
#> [7] 259.83755 -49.36017 803.87042 664.65502 1066.56606 832.10876
#> [13] 921.09703 708.17844 584.32970 666.78203 449.75382 563.19896
#> [19] 822.96480 533.40724 392.71320 1250.52803 141.94014 1032.36011
#> [25] 699.48592 415.15448 1230.80383 360.77385 939.43926 427.07940
#>
#> $deli
#> [1] 2041.7276 2102.3603 3153.6790 2253.6270 1389.6849 889.6343 1173.1534
#> [8] 1614.1015 1993.4468 1378.8796 807.7531 2412.1870 2545.7539 2041.5086
#> [15] 2720.2689 578.2671 1027.1884 1972.8876 1456.6372 2045.8364 1698.9477
#> [22] 1538.8655 1076.4195 2405.7123 1273.4507 1915.5241 3018.1125 1476.8084
#> [29] 700.7689 1541.3746
#>
#> $cheese
#> [1] 586.5153 352.0988 425.1849 395.0457 436.9520 193.9888 355.8510 378.5438
#> [9] 214.8330 310.7211 515.9006 398.2087 268.0164 205.9149 397.7948 389.1969
#> [17] 678.3098 574.4067 334.6408 336.5566 330.7829 402.7869 200.8166 472.5778
#> [25] 440.7484 363.8783 461.9439 366.2585 389.2016 441.0858
# You can also do it with pmap()
# Feed pmap() a list of the things you want to iterate over
# This does the same thing as map2() above
pmap(list(mean_sales, sd_sales), \(.x, .y) rnorm(30, .x, .y))
#> $dairy
#> [1] 5796.141 5611.484 7162.919 5027.586 6124.480 7031.901 4683.612 6223.506
#> [9] 5117.825 5432.135 1882.278 4823.226 4579.571 6470.582 4587.952 6625.621
#> [17] 3468.944 6188.743 3579.885 6193.145 5123.065 5053.477 4993.502 3522.434
#> [25] 3535.681 5705.973 4917.262 5001.409 3724.536 5677.403
#>
#> $meat
#> [1] 4736.672 8410.253 4213.788 5970.187 5062.590 6166.642 4530.490 7774.392
#> [9] 3197.219 6057.641 4106.305 6504.273 3004.272 3119.105 5707.722 5211.699
#> [17] 5057.762 7146.048 6027.152 3936.773 5980.077 4969.077 4011.683 4941.481
#> [25] 7749.998 4523.421 4429.622 5487.860 5241.544 5504.709
#>
#> $fish
#> [1] 1077.2922087 664.3992396 -94.0516098 1033.1168488 642.7037181
#> [6] 281.8817330 1312.7968147 680.3272343 740.7940443 279.5863394
#> [11] 1205.3849598 0.9968666 1219.3140410 992.2319192 615.1681292
#> [16] 1613.4185352 634.2736648 914.6779457 1075.0483452 751.3671131
#> [21] 749.7716172 823.2210680 1195.0128498 641.2005430 167.6428710
#> [26] 1307.7519666 693.6504176 974.2754732 1109.4577721 1127.0880319
#>
#> $deli
#> [1] 1960.4562 1599.0875 1973.5885 1747.1729 2161.3541 1656.4274 2105.8800
#> [8] 1063.1638 1758.8410 1654.0884 1390.4837 2137.1057 707.8833 1878.3749
#> [15] 2430.2976 2030.2897 2305.5022 2038.5225 1094.2663 1381.7758 1206.2534
#> [22] 1442.0891 2372.8524 659.7746 980.7443 1891.6903 2072.2370 1387.4277
#> [29] 806.1344 1706.7899
#>
#> $cheese
#> [1] 449.3391 468.2549 441.5325 485.7845 145.4496 413.6460 335.5230 141.5867
#> [9] 268.3779 236.5065 244.3030 309.7544 281.9910 276.9851 473.9348 461.6785
#> [17] 291.7884 505.8305 206.8490 404.4383 378.1601 314.0748 314.6305 385.6589
#> [25] 312.6995 604.0683 647.7857 402.0079 382.6572 425.8854
# The reason pmap() is helpful is that there's no map3() or map4(), etc.
# When you want to iterate over more than two things, you have to use pmap()
#
# Like here, if you want different numbers of random values for each product
# instead of hardcoding 30 like above, you can iterate across three vectors:
# the average, the standard deviation, and the n
n_sims <- c(30, 50, 10, 5, 20)
pmap(
list(mean_sales, sd_sales, n_sims),
\(sim_mean, sim_sd, sim_n) rnorm(sim_n, sim_mean, sim_sd)
)
#> $dairy
#> [1] 6693.905 5714.943 5832.067 5451.834 4413.806 5433.380 5329.867 5330.176
#> [9] 3241.697 5402.478 4341.237 5095.093 3399.808 4909.577 6228.329 6658.075
#> [17] 6558.241 6245.107 4692.691 5082.237 5540.482 6645.719 4903.239 3485.229
#> [25] 5057.145 5195.805 5733.390 6257.232 5174.344 7168.186
#>
#> $meat
#> [1] 4130.090 4372.595 3116.042 6267.654 7570.277 6033.013 4451.549 4888.003
#> [9] 4310.808 4380.928 4965.622 8351.922 5983.638 3945.802 5206.758 5419.650
#> [17] 4406.139 8302.713 4678.935 5250.405 4290.196 5517.314 2905.637 7996.581
#> [25] 5878.632 3970.786 5727.947 1213.952 6274.818 4068.267 7694.330 6340.665
#> [33] 4317.714 4958.674 2718.846 5090.947 6026.178 8255.268 8664.208 2780.296
#> [41] 5777.858 6581.447 5993.672 2876.855 5873.372 2624.844 8144.831 8464.173
#> [49] 6288.953 5031.721
#>
#> $fish
#> [1] 852.5548 856.9256 682.0329 380.9058 456.1242 1004.1867 482.6784
#> [8] 877.0729 1445.6654 621.2726
#>
#> $deli
#> [1] 774.1095 1400.2251 2053.9978 1774.7949 1449.3880
#>
#> $cheese
#> [1] 369.3466 542.8249 313.8997 538.6235 350.7178 266.4032 440.0691 157.7983
#> [9] 488.3640 516.7998 548.0659 329.4219 536.5104 233.8009 469.6049 416.3605
#> [17] 439.2675 371.2192 465.4377 359.9646Created on 2024-10-07 with reprex v2.1.1