powered by
Utility function to construct a sparse aggregation matrix from a factor
aggrMatrix(fac, w = 1, mean = FALSE, facnames = FALSE)
A sparse aggregation matrix of class tabMatrix.
tabMatrix
factor variable.
vector of weights associated with the levels of fac.
fac
if TRUE, aggregation will produce (weighted) means instead of sums.
TRUE
whether the factor levels should be used as column names for the aggregation matrix.
n <- 1000 f <- sample(1:100, n, replace=TRUE) x <- runif(n) M <- aggrMatrix(f) all.equal(crossprod_mv(M, x), as.vector(tapply(x, f, sum)))
Run the code above in your browser using DataLab