aggrMatrix: Utility function to construct a sparse aggregation matrix...

View source: R/MatrixUtils.R

aggrMatrixR Documentation

Utility function to construct a sparse aggregation matrix from a factor

Description

Utility function to construct a sparse aggregation matrix from a factor

Usage

aggrMatrix(fac, w = 1, mean = FALSE, facnames = FALSE)

Arguments

fac

factor variable.

w

vector of weights associated with the levels of fac.

mean

if TRUE, aggregation will produce (weighted) means instead of sums.

facnames

whether the factor levels should be used as column names for the aggregation matrix.

Value

sparse aggregation matrix of class tabMatrix.

Examples

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)))


mcmcsae documentation built on Oct. 11, 2023, 1:06 a.m.