fmapg: Apply a function to groups of a data frame in parallel

View source: R/fmapg.R

fmapgR Documentation

Apply a function to groups of a data frame in parallel

Description

Applies a function '.f' to each group of rows in a data frame '.df', where grouping is defined by one or more variables in 'by'. Each group is passed as a data frame to '.f'. Supports parallelism and optional progress display.

Usage

fmapg(.df, .f, by, ncores = NULL, pb = FALSE, ...)

Arguments

.df

A data frame to group and apply the function over.

.f

A function to apply to each group. The function should accept a data frame (a group).

by

A character vector of column names in '.df' used for grouping.

ncores

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

pb

Logical. Whether to show a progress bar. Default is 'FALSE'.

...

Additional arguments passed to '.f'.

Value

A list of results, one for each group defined by 'by'.

Examples

# Group-wise mean of Sepal.Length in iris dataset
fmapg(iris, function(df) mean(df$Sepal.Length), by = "Species")

# Group-wise model fitting with progress and parallelism

fmapg(mtcars, function(df) lm(mpg ~ wt, data = df), by = "cyl", ncores = 2, pb = TRUE)



functionals documentation built on Aug. 8, 2025, 7:32 p.m.