Learn R Programming

functionals (version 0.5.0)

fmapn: Apply a function over multiple argument lists in parallel

Description

Applies a function `.f` over multiple aligned lists in `.l`. Each element of `.l` should be a list or vector of the same length. Each call to `.f` receives one element from each list. Supports parallel execution and progress display.

Usage

fmapn(.l, .f, ncores = NULL, pb = FALSE, ...)

Value

A list of results obtained by applying `.f` to each tuple from `.l`.

Arguments

.l

A list of vectors or lists. All elements must be of equal length.

.f

A function to apply. It must accept as many arguments as there are elements in `.l`.

ncores

Integer. Number of cores to use for parallel processing. Default is `NULL` (sequential).

pb

Logical. Whether to display a progress bar. Default is `FALSE`.

...

Additional arguments passed to `.f`.

Examples

Run this code
# Fit a linear model for each response variable using the same predictor
df <- data.frame(
  y1 = rnorm(100),
  y2 = rnorm(100),
  x = rnorm(100)
)

# List of formulas and data
formulas <- list(y1 ~ x, y2 ~ x)
data_list <- list(df, df)

fmapn(list(formula = formulas, data = data_list), function(formula, data) {
  lm(formula, data = data)
})

# Extract model summaries in parallel
models <- fmapn(list(formula = formulas, data = data_list), function(formula, data) {
  summary(lm(formula, data = data))$r.squared
})

Run the code above in your browser using DataLab