selector_matrix: Selector matrix

Description Usage Arguments Details Value See Also Examples

View source: R/estimation.R

Description

Create a selector matrix from a list of categories.

Usage

1
selector_matrix(x, v, sparse = FALSE)

Arguments

x

A list of vectors used for indexing (i.e., a list of integer positions, names, or logical values).

v

An object that can be indexed with `[`, usually a numeric vector.

sparse

Should the result be a sparse matrix from Matrix?

Details

This function is essentially an application of lapply to a list of vectors for indexing that returns a matrix, the ij-th column of which has a 1 if the i-th element of x selects the j-th element of v, 0 otherwise. The canonical application of this function has a numeric vector for v so that the resulting selector matrix can be multiplied with v to create a matrix where each column contains only certain values of v.

An error is returned if any element of x indexes out of the bounds of v. This ensures that a matrix can be safely returned.

Value

An integer matrix of 0's and 1's with a column for each element in x and a row for each element in v.

See Also

See pias_matrix for a useful application when making a price index. See pias_class for an easy way to build x.

Examples

1
2
3
4
5
6
7
## Not run: 
v <- c(a = 0.25, b = 0.2, c = 0.15, d = 0.4)
x <- list(smaller = c('a', 'b'), larger = c('c', 'd'), total = c('a', 'b', 'c', 'd'))
sm <- selector_matrix(x, v)
sm * v

## End(Not run)

marberts/ppd documentation built on March 27, 2020, 7:21 p.m.