Learn R Programming

specmine (version 4.0.0)

apply_by_groups: 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
)

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.

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.

Examples

Run this code
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)

Run the code above in your browser using DataLab