Skip to contents

Methods to extract and replace index values like a matrix.

Usage

# S3 method for piar_index
[(x, i, j, ...)

# S3 method for piar_index
[(x, i, j, ...) <- value

Arguments

x

A price index, as made by, e.g., elemental_index().

i, j

Indices for the levels and time periods of a price index. See details.

...

Not currently used.

value

A numeric vector or price index. See details.

Value

A price index that inherits from the same class as x.

Details

The extraction method treats x like a matrix of index values with (named) rows for each level and columns for each time period in x. Unlike a matrix, dimensions are never dropped as subscripting x always returns an index object. This means that subscripting with a matrix is not possible, and only a "submatrix" can be extracted. As x is not an atomic vector, subscripting with a single index like x[1] extracts all time periods for that level.

The replacement method similarly treat x like a matrix. If value is an index object with the same number of time periods as x[i, j] and it inherits from the same class as x, then the index values and percent-change contributions of x[i, j] are replaced with those for the corresponding levels of value. If value is not an index, then it is coerced to a numeric vector and behaves the same as replacing values in a matrix. Note that replacing the values of an index will remove the corresponding percent-change contributions (if any). Unlike extraction, it is possible to replace value in x using a logical matrix or a two-column matrix of indices.

Examples

index <- as_index(matrix(1:6, 2))

index["1", ]
#> Period-over-period price index for 1 levels over 3 time periods 
#>   1 2 3
#> 1 1 3 5

index[, 2]
#> Period-over-period price index for 2 levels over 1 time periods 
#>   2
#> 1 3
#> 2 4

index[1, ] <- 1 # can be useful for doing specific imputations

index
#> Period-over-period price index for 2 levels over 3 time periods 
#>   1 2 3
#> 1 1 1 1
#> 2 2 4 6