Skip to contents

Performs t-tests comparing two groups for each row (or column) of a tidymatrix. Automatically detects groups and applies multiple testing correction.

Usage

compute_ttest(
  .data,
  group_col,
  control = NULL,
  treatment = NULL,
  log2 = TRUE,
  adjust = "fdr",
  add_to_data = FALSE,
  prefix = NULL,
  return_tibble = TRUE,
  ...
)

Arguments

.data

A tidymatrix object with rows or columns active

group_col

Character. Name of column in metadata containing group labels

control

Character. Label for the control group. If NULL, it is inferred: when group_col is a factor, the first level present in the data; otherwise the first value in sorted order. If only treatment is given, control is the other group.

treatment

Character. Label for the treatment group. If NULL, the group that is not control. When either group is inferred, a message reports the comparison being made. Set both explicitly to silence it.

log2

Logical. If TRUE (default), computes log2 fold change. If FALSE, computes raw fold change

adjust

Character. Method for p-value adjustment. Default "fdr". See ?p.adjust for options. Use "none" for no adjustment

add_to_data

Logical. If TRUE, adds results to row_data/col_data and returns modified tidymatrix. If FALSE (default), returns data.frame

prefix

Character. Optional prefix for result column names when add_to_data = TRUE

return_tibble

Logical. If TRUE (default), returns tibble. If FALSE, returns data.frame. Only applies when add_to_data = FALSE

...

Additional arguments passed to t.test()

Value

A data.frame/tibble with columns: identifiers, p.value, log2fc (or fc), and p.adj (if adjustment applied)

Examples

mat <- matrix(rnorm(100, mean = 10), nrow = 10, ncol = 10)
col_data <- data.frame(
  sample = paste0("S", 1:10),
  condition = rep(c("Control", "Treatment"), each = 5)
)
row_data <- data.frame(gene = paste0("Gene", 1:10))
tm <- tidymatrix(mat, row_data, col_data)

# Simple t-test
results <- tm |>
  activate(rows) |>
  compute_ttest(group_col = "condition")
#> Comparing condition: treatment = 'Treatment' vs control = 'Control'

# With specific group labels
results <- tm |>
  activate(rows) |>
  compute_ttest(
    group_col = "condition",
    control = "Control",
    treatment = "Treatment"
  )