50% off: Unlimited data and AI learning.
State of Data and AI Literacy Report 2025

Matrix.utils (version 0.5)

aggregate.Matrix: Compute summary statistics of a Matrix.

Description

Similar to aggregate. Splits the matrix into groups as specified by groupings, which can be one or more variables. Aggregation function will be applied to all columns in data, or as specified in formula. Warning: groupings will be made dense if it is sparse, though data will not.

Usage

## S3 method for class 'Matrix':
aggregate(x, groupings = NULL, form = NULL, fun = "sum",
  ...)

Arguments

x
a Matrix or matrix-like object
groupings
an object coercible to a group of factors defining the groups
fun
name of aggregation function to be applied to all columns in data
...
arguments to be passed to or from methods. Currently ignored

See Also

summarise summarise aggregate

Examples

Run this code
skus<-Matrix(as.matrix(data.frame(
   orderNum=sample(1000,10000,TRUE),
   sku=sample(1000,10000,TRUE),
   amount=runif(10000))),sparse=TRUE)
a<-aggregate.Matrix(skus[,'amount'],skus[,'sku',drop=FALSE])

m<-rsparsematrix(1000000,100,.001)
labels<-as.factor(sample(1e4,1e6,TRUE))
b<-aggregate.Matrix(m,labels)

orders<-data.frame(orderNum=as.factor(sample(1e6, 1e7, TRUE)),
   sku=as.factor(sample(1e3, 1e7, TRUE)),
   customer=as.factor(sample(1e4,1e7,TRUE)),
   state = sample(letters, 1e7, TRUE), amount=runif(1e7))
system.time(d<-aggregate.Matrix(orders[,'amount',drop=FALSE],orders$orderNum))

Run the code above in your browser using DataLab