Construct a sparse matrix with one row per id and one column per
group, containing a 1 where an id belongs to a group. Right-multiplying a
connectivity matrix by grouping_matrix(colnames(M), group) sums the
columns of M within each group (e.g. collapsing partner neurons to
cell types).
Arguments
- ids
Character vector (or coercible) of identifiers, typically the row or column names of a matrix to be aggregated.
- group
Grouping labels. Either a vector parallel to
idsor a named vector that will be looked up byids. Ids whose group isNAare dropped (contribute no 1s), so they vanish from any aggregation.
Value
A sparse Matrix of dimension
length(ids) x number of groups with ids as row names and
group labels as column names.