Learn R Programming

specmine (version 4.0.0)

apply_by_group: Apply by group

Description

Applies a function to the variables of samples belonging to a given group.

Usage

apply_by_group(dataset, fn.to.apply, metadata.var, var.value)

Value

A named vector or matrix, depending on the output of `fn.to.apply`, containing the values obtained by applying the function to each variable across the samples belonging to the selected group.

Arguments

dataset

Dataset to analyze.

fn.to.apply

Function to apply.

metadata.var

Metadata variable used to define the group.

var.value

Value or values of the metadata variable to select.

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_group(dataset, mean, "group", "control")

Run the code above in your browser using DataLab