Skip to contents

Sum and count positive sparse entries by column group

Usage

sparse_group_stats(x, group, n_groups, transform = c("identity", "expm1"))

Arguments

x

A dgCMatrix containing finite, non-negative values.

group

A vector of one-based integer codes, one per column of x.

n_groups

Number of output groups, including empty groups.

transform

Transform applied before summation: "identity" or "expm1".

Value

A list with dense numeric matrices sum and detected, one row per input row and one column per group code. Input row names are preserved. Counts omit stored and implicit zeros. An overflowing sum is Inf.

Examples

x <- Matrix::sparseMatrix(i = c(1, 2), j = c(1, 2), x = c(2, 3))
sparse_group_stats(x, group = c(2L, 1L), n_groups = 2L)
#> $sum
#>      [,1] [,2]
#> [1,]    0    2
#> [2,]    3    0
#> 
#> $detected
#>      [,1] [,2]
#> [1,]    0    1
#> [2,]    1    0
#>