Learn R Programming

functionals (version 0.5.0)

fmapg: 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, ...)

Value

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

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`.

Examples

Run this code
# 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
# \donttest{
fmapg(mtcars, function(df) lm(mpg ~ wt, data = df), by = "cyl", ncores = 2, pb = TRUE)
# }

Run the code above in your browser using DataLab