matrix_grouped_column_aggregate | R Documentation |
fast grouped matrix aggregation using data.table
matrix_grouped_column_aggregate(x, x_rowgroups, FUN)
x |
a matrix |
x_rowgroups |
grouping variable |
FUN |
function to apply, e.g. |
a matrix, where rownames match the (unique values of the) grouping variable
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.