sum_row_groups | R Documentation |
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.
sum_row_groups(m, f, row_names = levels(f)) sum_col_groups(m, f, col_names = levels(f))
m |
matrix (or Matrix) |
f |
factor or something coercible to one. Must have as many elements as
|
row_names |
used to name the rows of the result of
|
col_names |
Similarly, for |
Note that ordinary rowSums
corresponds to sum_col_groups
with a
one-level factor, and conversely.
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)
.
model.matrix
and
sparse.model.matrix
, which this function uses to
transform the factor f
into an indicator matrix to multiply by
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.