Skip to contents

Compute statistics for the active dimension and add them as columns to the corresponding metadata. Requires rows or columns to be active.

Usage

add_stats(.data, ..., .fns = NULL, .names = NULL)

Arguments

.data

A tidymatrix object with rows or columns active

...

Functions to compute statistics (unquoted names like mean, var, sd)

.fns

Alternative way to specify functions as a list

.names

Names for the new columns. If NULL, uses function names.

Value

A tidymatrix object with statistics added to metadata

Examples

mat <- matrix(rnorm(20), nrow = 4, ncol = 5)
tm <- tidymatrix(mat)

# Add row statistics
tm <- tm |>
  activate(rows) |>
  add_stats(mean, var, sd)

# Now row_data has columns: mean, var, sd

# Add column statistics
tm <- tm |>
  activate(columns) |>
  add_stats(median, min, max)

# Custom names
tm <- tm |>
  activate(rows) |>
  add_stats(mean, var, .names = c("row_mean", "row_var"))