sum_row_groups: Sum ragged groups of matrix rows or columns

sum_row_groupsR Documentation

Sum ragged groups of matrix rows or columns

Description

Given a matrix and a factor, yields a matrix where all rows (sum_row_groups) or columns (sum_col_groups) corresponding to the same factor level have been summed. This is just a convenience wrapper for matrix multiplication. However, this is a frequent operation with models of this kind (e.g. analyzing topic distributions over metadata categories), and with the large matrices one often deals with in these cases, converting to a data frame and using ordinary tapply or dplyr grouping can be laborious.

Usage

sum_row_groups(m, f, row_names = levels(f))

sum_col_groups(m, f, col_names = levels(f))

Arguments

m

matrix (or Matrix)

f

factor or something coercible to one. Must have as many elements as m has rows (for sum_row_groups) or columns (for sum_col_groups).

row_names

used to name the rows of the result of sum_row_groups. By default, the levels of f are used. Set to NULL to blank these out

col_names

Similarly, for sum_col_groups

Details

Note that ordinary rowSums corresponds to sum_col_groups with a one-level factor, and conversely.

Value

a matrix (or Matrix) with the same number of columns (rows) as m, but rows (columns) corresponding to the same level of f summed. These rows (columns) are in the same order as levels(f).

See Also

model.matrix and sparse.model.matrix, which this function uses to transform the factor f into an indicator matrix to multiply by


agoldst/dfrtopics documentation built on July 15, 2022, 4:13 p.m.