Skip to contents

Performs Wilcoxon rank-sum tests (Mann-Whitney U test) comparing two groups for each row (or column) of a tidymatrix. This is a non-parametric alternative to the t-test that doesn't assume normal distribution.

Usage

compute_wilcox(
  .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 wilcox.test()

Value

A data.frame/tibble with columns: identifiers, p.value, log2fc (or fc), median_diff, 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)

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