Skip to contents

Repeat an expression (usually involving random number generation) multiple times. Optionally, organize the results into a data.frame that stacks the output from all replications of the expression.

Usage

repeat_and_stack(n, expr, stack = TRUE)

Arguments

n

Number of times to repeat the expression

expr

An expression to be evaluated.

stack

Logical value indicating whether to organize the results into a data.frame.

Value

If stack = TRUE (the default), the results of each evaluation of expr will be stacked together using rbind. If stack = FALSE, a list of length n with entries corresponding to the output of each replication of expr.

Examples

repeat_and_stack(n = 3, data.frame(x = rexp(2)))
#>           x
#> 1 1.7272566
#> 2 4.3115028
#> 3 1.2542130
#> 4 0.7333867
#> 5 0.4364060
#> 6 2.9640214

repeat_and_stack(n = 3, data.frame(x = rexp(2)), stack = FALSE)
#> [[1]]
#>           x
#> 1 2.6859047
#> 2 0.5160765
#> 
#> [[2]]
#>           x
#> 1 0.4349498
#> 2 1.2505684
#> 
#> [[3]]
#>            x
#> 1 0.04620374
#> 2 0.34424903
#>