| rename_via_pattern_byname | R Documentation |
It is sometimes helpful to rename row or column names
for a list of matrices via a regexp_pattern.
When a is a matrix or a list of matrices,
regexp_pattern indicates which characters
are replaced by replacement.
rename_via_pattern_byname(
a,
margin = list(c(1, 2)),
regexp_pattern = "$^",
replacement,
pieces = "all",
prepositions = RCLabels::prepositions_list,
notation = RCLabels::bracket_notation,
...
)
a |
A matrix or list of matrices. |
margin |
The margin on which replacements are performed.
Default is |
regexp_pattern |
The regular expression pattern that will be replaced in the row or column labels. Default is "$^", meaning nothing will be matched. |
replacement |
The string to replace the |
pieces |
The pieces of labels to be searched for |
prepositions |
Prepositions to use while searching for
|
notation |
The notation used for for searching |
... |
Other arguments passed to |
Note that margin can be a rowtype or coltype string
which will be de-referenced to the integer margin
(1 for rows or 2 for columns).
Internally, this function calls RCLabels::replace_by_pattern.
A modified version of a.
ma <- matrix(c(1, 2), nrow = 2,
dimnames = list(c("Natural gas [from Supply]",
"row2"),
"col")) |>
setrowtype("Product") |> setcoltype("Industry")
mb <- matrix(c(1, 2), nrow = 2,
dimnames = list(c("Natural gas [from Supply]",
"Fuel oil [from Supply]"),
"col")) |>
setrowtype("Product") |> setcoltype("Industry")
ma |>
rename_via_pattern_byname(regexp_pattern = " [from Supply]",
replacement = " bogus",
fixed = TRUE)
list(ma, mb) |>
rename_via_pattern_byname(margin = 1,
regexp_pattern = " [from Supply]",
replacement = " from Supply",
fixed = TRUE)
res1 <- tibble::tibble(m = list(ma, mb)) |>
dplyr::mutate(
m1 = .data[["m"]] |>
rename_via_pattern_byname(regexp_pattern = " [from Supply]",
replacement = "",
fixed = TRUE)
)
res1$m1
# Transpose mb and use a string for the margin.
# The string (in this case "Product")
# is dereferenced to an integer margin.
# In this case, the rownames of the first matrix
# and the colnames of the second matrix are replaced,
# because those are on the "Product" margin.
res2 <- tibble::tibble(m = list(ma,
transpose_byname(mb))) |>
dplyr::mutate(
m2 = .data[["m"]] |>
rename_via_pattern_byname(margin = "Product",
regexp_pattern = " [from Supply]",
replacement = "",
fixed = TRUE)
)
rowtype(res2$m2[[1]])
coltype(res2$m2[[2]])
res2$m2
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.