apply_by_groups: Apply by groups

apply_by_groupsR Documentation

Apply by groups

Description

Applies a function to groups defined by a metadata variable.

Usage

apply_by_groups(
  dataset,
  metadata.var,
  fn.to.apply = "mean",
  variables = NULL,
  variable.bounds = NULL
)

Arguments

dataset

Dataset to analyze.

metadata.var

Metadata variable used to define groups.

fn.to.apply

Function to apply.

variables

Variables to include.

variable.bounds

Optional numeric bounds for variables.

Value

A matrix-like object with one row per selected variable and one column per group defined by 'metadata.var'. Each cell contains the result of applying 'fn.to.apply' to the values of that variable within the corresponding group.

Examples

data <- matrix(
  c(1, 2, 3, 4, 5, 6),
  nrow = 2,
  dimnames = list(c("x1", "x2"), c("s1", "s2", "s3"))
)
metadata <- data.frame(
  group = c("control", "control", "case"),
  row.names = c("s1", "s2", "s3")
)
dataset <- list(data = data, metadata = metadata)
apply_by_groups(dataset, "group", mean)


specmine documentation built on Aug. 5, 2026, 5:06 p.m.