apply_batch: Apply a function across simulation results

View source: R/workflows.R

apply_batchR Documentation

Apply a function across simulation results

Description

Apply a function across simulation results

Usage

apply_batch(inputs, fun, ..., id = "batch_id")

Arguments

inputs

List with at least one element, commonly data frames or simulation results. Element names label the batches; when inputs is unnamed the positions "1", "2", ... are used instead.

fun

Function applied to each element. When fun has a data argument, as lm() and glm() do, the element is passed as data; otherwise it is the first argument. fun returns a base data.frame, or a fitted model with named coefficients, which becomes a data frame with columns term and estimate. Every call must give the same columns, otherwise the run is an error.

...

Arguments passed to fun.

id

Single string naming the batch-label column, default "batch_id".

Value

A base data.frame stacking the fun outputs by row, with the batch label in a leading column named by id, so one row per row of each output.

Examples

inputs <- list(
  small = simulate_ttest(n_a = 20, n_b = 20, mean_a = 0, mean_b = 0.5, seed = 1),
  large = simulate_ttest(n_a = 60, n_b = 60, mean_a = 0, mean_b = 0.5, seed = 2)
)
apply_batch(inputs, fun = summary)

# A model function is applied to each data set and tidied to coefficients.
datasets <- simulate_regression(seed = 1, batch = 3)
apply_batch(datasets, lm, formula = outcome ~ x1 + x2)

simulab documentation built on Oct. 15, 2026, 5:08 p.m.