Skip to contents

Create a grouped tidymatrix for use with summarize(). Groups are created based on the active metadata component (row_data or col_data).

Usage

# S3 method for class 'tidymatrix'
group_by(.data, ..., .add = FALSE, .drop = TRUE)

Arguments

.data

A tidymatrix object

...

Variables to group by (unquoted names or expressions)

.add

When FALSE (default), group_by() will override existing groups. When TRUE, add to existing groups.

.drop

Drop groups with zero observations

Value

A grouped_tidymatrix object

Examples

library(dplyr, warn.conflicts = FALSE)
mat <- matrix(rnorm(20), nrow = 5, ncol = 4)
row_data <- data.frame(
  id = 1:5,
  group = c("A", "A", "B", "B", "C")
)
tm <- tidymatrix(mat, row_data)

# Group by a variable
tm_grouped <- tm |>
  activate(rows) |>
  group_by(group)

# Multiple grouping variables
row_data2 <- data.frame(
  id = 1:5,
  condition = c("ctrl", "ctrl", "treat", "treat", "treat"),
  batch = c(1, 2, 1, 2, 1)
)
tm2 <- tidymatrix(mat, row_data2)
tm_grouped2 <- tm2 |>
  activate(rows) |>
  group_by(condition, batch)