matrix_grouped_column_aggregate: fast grouped matrix aggregation using data.table

View source: R/util_generic.R

matrix_grouped_column_aggregateR Documentation

fast grouped matrix aggregation using data.table

Description

fast grouped matrix aggregation using data.table

Usage

matrix_grouped_column_aggregate(x, x_rowgroups, FUN)

Arguments

x

a matrix

x_rowgroups

grouping variable

FUN

function to apply, e.g. sum

Value

a matrix, where rownames match the (unique values of the) grouping variable

Examples

## Not run: 
x = my_integer_matrix
x_rowgroups = my_grouping_var
result = matrix_grouped_column_aggregate(x, x_rowgroups, FUN = sum)

# validate this function yields the same as (slower) stats::aggregate()
df = stats::aggregate(x, by = list(groupid = x_rowgroups), FUN = sum)
i = match(rownames(result), df$groupid)
stopifnot(!is.na(i))
stopifnot(count_detect == as.matrix(df[,-1])[i,])
stopifnot(rownames(count_detect) == df$groupid[i])

## End(Not run)

ftwkoopmans/msdap documentation built on March 5, 2025, 12:15 a.m.