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 0.01984866
#> 2 0.11961758
#> 3 0.56949077
#> 4 0.46020274
#> 5 0.53691438
#> 6 1.04824329

repeat_and_stack(n = 3, data.frame(x = rexp(2)), stack = FALSE)
#> [[1]]
#>          x
#> 1 1.022214
#> 2 0.172877
#> 
#> [[2]]
#>          x
#> 1 3.142675
#> 2 2.588006
#> 
#> [[3]]
#>            x
#> 1 0.05995107
#> 2 1.63208356
#>