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
# Works when renaming to the piece results in identical row or col names.
b <- matrix(1:6, nrow = 3, ncol = 2,
dimnames = list(c("a [from b]", "c [from d]", "c [from e]"),
c("c1", "c2")))
b
# This aggregation works, because the "c" rows
# are aggregated before applying the aggregation_map,
# which, itself, does NOT aggregate the "c" rows.
b %>%
aggregate_pieces_byname(piece = "noun",
margin = 1,
inf_notation = FALSE,
notation = RCLabels::bracket_notation,
aggregation_map = list(f = c("a", "b")))
Run the code above in your browser using DataLab