Learn R Programming

functionals (version 0.5.0)

fmapr: Apply a function row-wise on a data frame with parallelism

Description

Applies a function `.f` to each row of a data frame `.df`, with optional parallelism and progress bar. Each row is converted to a named list before being passed to `.f`, enabling flexible access to variables by name.

Usage

fmapr(.df, .f, ncores = NULL, pb = FALSE, ...)

Value

A list of results returned by applying `.f` to each row as a list.

Arguments

.df

A data frame whose rows will be iterated over.

.f

A function applied to each row, which receives a named list.

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
df <- data.frame(name = c("Mister", "Hipster"), age = c(30, 25))

# Create personalized messages
fmapr(df, function(row) paste(row$name, "is", row$age, "years old"))

# Row-wise model formulas
formulas <- data.frame(
  response = c("y1", "y2"),
  predictor = c("x1", "x2"),
  stringsAsFactors = FALSE
)

fmapr(formulas, function(row) {
  reformulate(row$predictor, row$response)
})

Run the code above in your browser using DataLab