Skip to contents

Count observations within existing groups. This is a wrapper around summarize(n = n()).

Usage

# S3 method for class 'grouped_tidymatrix'
tally(x, wt = NULL, sort = FALSE, name = NULL)

Arguments

x

A grouped_tidymatrix object

wt

Frequency weights (not yet implemented)

sort

If TRUE, sort output in descending order of n

name

Name of count column (default: "n")

Value

A tidymatrix object with counts and aggregated matrix

Details

The matrix is aggregated with mean(), which requires a numeric matrix. dplyr::tally() takes no ..., so the aggregation function cannot be overridden here; call summarize(n = n(), .matrix_fn = ...) directly instead.

Examples

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

# Tally within groups
tm |>
  activate(rows) |>
  group_by(group) |>
  tally()
#> # A tidymatrix: 2 x 2 matrix
#> # Active: rows
#> #
#> # Row data: 2 rows x 2 columns
#> # Column data: 2 rows x 1 columns
#> #
#> # Active data (rows):
#>   group n
#> 1     A 5
#> 2     B 5