Evalulate expression n times then combine results into an array
raply(.n, .expr, .progress = "none", .drop = TRUE)number of times to evaluate the expression
expression to evaluate
name of the progress bar to use, see create_progress_bar
should extra dimensions of length 1 be dropped, simplifying the output. Defaults to TRUE
if results are atomic with same type and dimensionality, a vector, matrix or array; otherwise, a list-array (a list with dimensions)
This function runs an expression multiple times, and combines the
result into a data frame. If there are no results, then this function
returns a vector of length 0 (vector(0)).
This function is equivalent to replicate, but will always
return results as a vector, matrix or array.
Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. https://www.jstatsoft.org/v40/i01/.
raply(100, mean(runif(100)))
#> [1] 0.5317310 0.4451389 0.4989314 0.5560588 0.5194858 0.4767879 0.5249314
#> [8] 0.5139516 0.5103224 0.5017852 0.4656494 0.5114601 0.4686935 0.4590420
#> [15] 0.4879825 0.5436443 0.4896089 0.4887177 0.4269286 0.5480423 0.4921314
#> [22] 0.4855120 0.4919166 0.4849943 0.5107972 0.5135879 0.4644579 0.5407537
#> [29] 0.5108721 0.5053966 0.5130982 0.4810458 0.4622933 0.4422771 0.5362857
#> [36] 0.4832248 0.4988079 0.4859018 0.4826639 0.4679503 0.4715443 0.4888007
#> [43] 0.4826446 0.4826155 0.5200765 0.4939423 0.4786497 0.4853606 0.5505879
#> [50] 0.5308593 0.5028858 0.5484650 0.5256007 0.4910664 0.5017458 0.4537656
#> [57] 0.5198043 0.5232183 0.5294873 0.4975548 0.4890206 0.5112228 0.5022320
#> [64] 0.4475460 0.4155831 0.5104173 0.5261531 0.5007230 0.5473746 0.5210284
#> [71] 0.4541974 0.5154945 0.4225829 0.4925723 0.5600504 0.4876733 0.5132487
#> [78] 0.5209745 0.4241520 0.5386534 0.4978408 0.4929886 0.5092256 0.4989210
#> [85] 0.5294446 0.5335655 0.5234693 0.5385156 0.5287105 0.4796984 0.5413925
#> [92] 0.5417897 0.4459124 0.5241288 0.5039939 0.4551127 0.4756170 0.5422490
#> [99] 0.5620528 0.4993890
raply(100, each(mean, var)(runif(100)))
#> mean var
#> [1,] 0.5124088 0.08716067
#> [2,] 0.5024772 0.07312724
#> [3,] 0.4567556 0.08779334
#> [4,] 0.5182139 0.08515268
#> [5,] 0.5281596 0.08575127
#> [6,] 0.5401396 0.09100446
#> [7,] 0.5257646 0.09217320
#> [8,] 0.5207967 0.07256935
#> [9,] 0.5261162 0.08990626
#> [10,] 0.4780479 0.07549494
#> [11,] 0.4903572 0.07825513
#> [12,] 0.5400938 0.08778925
#> [13,] 0.5044624 0.07992639
#> [14,] 0.5115190 0.08303022
#> [15,] 0.5044567 0.07874990
#> [16,] 0.4816601 0.09097337
#> [17,] 0.5050915 0.08742484
#> [18,] 0.5124639 0.07340436
#> [19,] 0.4826640 0.08224670
#> [20,] 0.4967060 0.08585950
#> [21,] 0.4775386 0.09199948
#> [22,] 0.4573993 0.07262157
#> [23,] 0.5205984 0.07565487
#> [24,] 0.4980395 0.08615942
#> [25,] 0.4807608 0.08466940
#> [26,] 0.5028560 0.07542388
#> [27,] 0.4441010 0.09338380
#> [28,] 0.5202522 0.08271870
#> [29,] 0.4928871 0.07693578
#> [30,] 0.5492485 0.09366906
#> [31,] 0.4825496 0.08871602
#> [32,] 0.5152358 0.08857836
#> [33,] 0.4807806 0.07992234
#> [34,] 0.4762050 0.07914193
#> [35,] 0.5005903 0.08091323
#> [36,] 0.5630887 0.09706753
#> [37,] 0.4609621 0.09264949
#> [38,] 0.5105665 0.08611357
#> [39,] 0.5054260 0.09094606
#> [40,] 0.4370619 0.07542180
#> [41,] 0.4891369 0.08089026
#> [42,] 0.4977352 0.07294921
#> [43,] 0.5071714 0.09856033
#> [44,] 0.5140275 0.08308148
#> [45,] 0.4873581 0.07838112
#> [46,] 0.5097723 0.08042420
#> [47,] 0.4865739 0.07151353
#> [48,] 0.5022427 0.08870775
#> [49,] 0.4633556 0.08912307
#> [50,] 0.4663070 0.07022731
#> [51,] 0.5020464 0.08710654
#> [52,] 0.4598378 0.06778292
#> [53,] 0.4896461 0.08812545
#> [54,] 0.4916839 0.08576865
#> [55,] 0.4388753 0.09561120
#> [56,] 0.4828898 0.07123389
#> [57,] 0.4897420 0.08891322
#> [58,] 0.5120279 0.09222677
#> [59,] 0.4790271 0.08532807
#> [60,] 0.4966256 0.07534078
#> [61,] 0.5013668 0.09277528
#> [62,] 0.5706684 0.08049826
#> [63,] 0.4661658 0.08095467
#> [64,] 0.5245839 0.06969066
#> [65,] 0.4927128 0.09251380
#> [66,] 0.5044021 0.08600039
#> [67,] 0.5178604 0.08644521
#> [68,] 0.5300222 0.08573846
#> [69,] 0.5205664 0.08209059
#> [70,] 0.5698498 0.07630739
#> [71,] 0.4959237 0.08584767
#> [72,] 0.5218696 0.08643356
#> [73,] 0.5348509 0.08439727
#> [74,] 0.5230110 0.08346826
#> [75,] 0.5011537 0.08055787
#> [76,] 0.5232796 0.08268019
#> [77,] 0.5042352 0.07895841
#> [78,] 0.5034468 0.09421608
#> [79,] 0.5337705 0.06666032
#> [80,] 0.5200538 0.08094163
#> [81,] 0.5182493 0.08230240
#> [82,] 0.4993719 0.08318841
#> [83,] 0.5074710 0.07801170
#> [84,] 0.4979873 0.08771768
#> [85,] 0.5023582 0.07535816
#> [86,] 0.4133386 0.09256521
#> [87,] 0.5233300 0.07172255
#> [88,] 0.4759483 0.08180530
#> [89,] 0.5200138 0.07255497
#> [90,] 0.5072789 0.09231398
#> [91,] 0.4556998 0.08997599
#> [92,] 0.4733632 0.09113931
#> [93,] 0.4868581 0.08141009
#> [94,] 0.5242450 0.08340398
#> [95,] 0.4900223 0.07556394
#> [96,] 0.5282424 0.07831873
#> [97,] 0.4757805 0.07540769
#> [98,] 0.4867519 0.08057511
#> [99,] 0.4732164 0.08610292
#> [100,] 0.5163106 0.08747746
raply(10, runif(4))
#> 1 2 3 4
#> [1,] 0.9232227 0.7524017 0.2265271 0.39899366
#> [2,] 0.7274133 0.7272453 0.9438763 0.09961407
#> [3,] 0.3876880 0.7717237 0.4681167 0.69185158
#> [4,] 0.8637377 0.6435446 0.1766891 0.96643654
#> [5,] 0.1937629 0.3820120 0.7596560 0.96053554
#> [6,] 0.1172657 0.5938016 0.6560719 0.94579697
#> [7,] 0.6873582 0.9908877 0.5922075 0.93931979
#> [8,] 0.8097768 0.5985291 0.6605421 0.17259128
#> [9,] 0.1671973 0.6360236 0.3539980 0.56933753
#> [10,] 0.9716195 0.7946353 0.1934265 0.57389254
raply(10, matrix(runif(4), nrow=2))
#> , , 1
#>
#> 1 2
#> [1,] 0.2690048 0.8203000
#> [2,] 0.3522242 0.9380419
#> [3,] 0.6815490 0.2792521
#> [4,] 0.1321905 0.4548232
#> [5,] 0.4820718 0.7074740
#> [6,] 0.9513625 0.1810211
#> [7,] 0.3116452 0.9144697
#> [8,] 0.8429307 0.8190748
#> [9,] 0.5725469 0.7192256
#> [10,] 0.2047378 0.8103922
#>
#> , , 2
#>
#> 1 2
#> [1,] 0.21404031 0.8535246
#> [2,] 0.30463837 0.7815492
#> [3,] 0.81247347 0.3896659
#> [4,] 0.06669945 0.9339880
#> [5,] 0.84783450 0.6153607
#> [6,] 0.18189629 0.6006893
#> [7,] 0.64644402 0.3102201
#> [8,] 0.56423579 0.5972394
#> [9,] 0.29663454 0.7623214
#> [10,] 0.59087416 0.8232473
#>
# See the central limit theorem in action
hist(raply(1000, mean(rexp(10))))
hist(raply(1000, mean(rexp(100))))
hist(raply(1000, mean(rexp(1000))))