aggregate_pieces_byname: Aggregate a matrix by pieces of row and/or column names

View source: R/Unary.R

aggregate_pieces_bynameR Documentation

Aggregate a matrix by pieces of row and/or column names

Description

Aggregate a matrix (or list of matrices or a column in a matsindf data frame) by pieces of the row and column names.

Usage

aggregate_pieces_byname(
  a,
  piece,
  margin = c(1, 2),
  notation,
  prepositions = RCLabels::prepositions,
  aggregation_map = NULL,
  pattern_type = "exact"
)

Arguments

a

A matrix or list of matrices

piece

See rename_to_piece_byname().

margin

See rename_to_piece_byname().

notation

See rename_to_piece_byname().

prepositions

See rename_to_piece_byname().

aggregation_map

See aggregate_byname().

pattern_type

See RCLabels::make_or_pattern().

Details

This is a convenience function that bundles two others for common use cases: rename_to_piece_byname() followed by aggregate_byname().

aggregation_map should aggregate according to pieces, not according to the full, original row and/or column names.

Value

A version of a with rows and/or columns aggregated according to aggregation_map.

Examples

a <- matrix(c(1, 2, 3, 
              4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE, 
            dimnames = list(c("a [from b]", "c [from d]"), 
                            c("e [from f]", "g [from h]", "i [from j]")))
a %>%
  aggregate_pieces_byname(piece = "suff", 
                          notation = RCLabels::from_notation,
                          aggregation_map = list(rows = c("b", "d"), 
                                                 cols = c("h", "j")))
m <- matrix(c(1, 0, 0, 
              0, 1, 1, 
              0, 1, 1), nrow = 3, ncol = 3, byrow = TRUE, 
            dimnames = list(c("Gasoline [from Oil refineries]", 
                              "Electricity [from Main activity producer electricity plants]", 
                              "Electricity [from Hydro]"),
                            c("Automobiles", "LED lamps", "CFL lamps"))) %>%
  setrowtype("Product") %>% setcoltype("Industry")
mT <- transpose_byname(m)
# Aggregate the "Electricity" rows.
aggregate_pieces_byname(m, piece = "noun", margin = "Product",
                        notation = RCLabels::bracket_notation)
# Also works in a list.
aggregate_pieces_byname(a = list(m, mT), piece = "noun", 
                        margin = "Product",
                        notation = RCLabels::bracket_notation)
# Use an aggregation map
aggregate_pieces_byname(a = list(m, mT), piece = "noun", 
                        margin = "Product",
                        aggregation_map = list(list(final = c("Electricity", "Gasoline"))),
                        notation = RCLabels::bracket_notation)
# Also works in a data frame.
df <- tibble::tibble(m = list(m, mT), 
                     pce = "noun",
                     mgn = "Product",
                     agg_map = list(list(final = c("Electricity", "Gasoline"))), 
                     notn = list(RCLabels::bracket_notation)) %>%
  dplyr::mutate(
    agg = aggregate_pieces_byname(a = m, piece = pce, margin = mgn, 
                                  aggregation_map = agg_map,
                                  notation = notn)
  )
df$agg

matsbyname documentation built on April 2, 2022, 1:06 a.m.