It is often convenient to represent row and column names with notation that includes a prefix and a suffix, with corresponding separators or start-end string sequences. There are several functions that call notation_vec() to generate specialized versions or otherwise manipulate row and column names on their own or as row or column names.

  • notation_vec() Builds a vector of notation symbols in a standard format that is used by matsbyname in several places. By default, it builds a list of notation symbols that provides an arrow separator (" -> ") between prefix and suffix.

  • arrow_notation() Builds a list of notation symbols that provides an arrow separator (" -> ") between prefix and suffix.

  • paren_notation() Builds a list of notation symbols that provides parentheses around the suffix ("prefix (suffix)").

  • bracket_notation() Builds a list of notation symbols that provides square brackets around the suffix ("prefix [suffix]").

  • preposition_notation() Builds a list of notation symbols that provides (by default) square brackets around the suffix with a preposition ("prefix [preposition suffix]").

  • from_notation() Builds a list of notation symbols that provides (by default) square brackets around a "from" suffix ("prefix [from suffix]").

  • of_notation() Builds a list of notation symbols that provides (by default) square brackets around an "of" suffix ("prefix [of suffix]").

  • split_pref_suff() Splits prefixes from suffixes, returning each in a list with names pref and suff. If no prefix or suffix delimiters are found, x is returned in the pref item, unmodified, and the suff item is returned as "" (an empty string). If there is no prefix, and empty string is returned for the pref item. If there is no suffix, and empty string is returned for the suff item.

  • paste_pref_suff() paste0's prefixes and suffixes, the inverse of split_pref_suff().

  • flip_pref_suff() Switches the location of prefix and suffix, such that the prefix becomes the suffix, and the suffix becomes the prefix. E.g., "a -> b" becomes "b -> a" or "a [b]" becomes "b [a]".

  • keep_pref_suff() Selects only prefix or suffix, discarding notational elements and the rejected part.

  • switch_notation() Switches from one type of notation to another based on the from and to arguments. Optionally, prefix and suffix can be flipped.

  • switch_notation_byname() Switches matrix row and/or column names from one type of notation to another based on the from and to arguments. Optionally, prefix and suffix can be flipped.

If sep only is specified (default is " -> "), pref_start, pref_end, suff_start, and suff_end are set appropriately.

None of the strings in a notation vector are considered part of the prefix or suffix. E.g., "a -> b" in arrow notation means that "a" is the prefix and "b" is the suffix.

notation_vec(
  sep = " -> ",
  pref_start = "",
  pref_end = "",
  suff_start = "",
  suff_end = ""
)

arrow_notation()

paren_notation(suff_start = " (", suff_end = ")")

bracket_notation(suff_start = " [", suff_end = "]")

preposition_notation(preposition, suff_start = " [", suff_end = "]")

from_notation(preposition = "from", suff_start = " [", suff_end = "]")

of_notation(preposition = "of", suff_start = " [", suff_end = "]")

split_pref_suff(x, notation = arrow_notation())

paste_pref_suff(
  ps = list(pref = pref, suff = suff),
  pref = NULL,
  suff = NULL,
  notation = arrow_notation()
)

flip_pref_suff(x, notation = arrow_notation())

keep_pref_suff(x, keep = c("pref", "suff"), notation)

switch_notation(x, from, to, flip = FALSE)

switch_notation_byname(a, margin = c(1, 2), from, to, flip = FALSE)

Arguments

sep

A string separator between prefix and suffix. Default is " -> ".

pref_start

A string indicating the start of a prefix. Default is NULL.

pref_end

A string indicating the end of a prefix. Default is the value of sep.

suff_start

A string indicating the start of a suffix. Default is the value of sep.

suff_end

A string indicating the end of a suffix. Default is NULL.

preposition

A string used to indicate position for energy flows, typically "from" or "to" in different notations.

x

A string or list of strings to be operated upon.

notation

A notation vector generated by one of the *_notation() functions, such as notation_vec(), arrow_notation(), or bracket_notation(). Default is arrow_notation().

ps

A list of prefixes and suffixes in which each item of the list is itself a list with two items named pref and suff.

pref

A string or list of strings that are prefixes. Default is NULL.

suff

A string of list of strings that are suffixes. Default is NULL.

keep

Tells which

from

The notation to switch away from.

to

The notation to switch to.

flip

A boolean that tells whether to also flip the notation. Default is FALSE.

a

A matrix or list of matrices whose row and/or column notation is to be changed.

margin

1 For rows, 2 for columns, or c(1, 2) for both rows and columns. Default is c(1, 2).

Value

For notation_vec(), arrow_notation(), and bracket_notation(), a string vector with named items pref_start, pref_end, suff_start, and suff_end; For split_pref_suff(), a string list with named items pref and suff. For paste_pref_suff(), split_pref_suff(), and switch_notation(), a string list in notation format specified by various notation arguments, including from, and to. For keep_pref_suff, one of the prefix or suffix or a list of prefixes or suffixes. For switch_row_col_notation_byname(), matrices with row and column names with switched notation, per arguments.

Examples

notation_vec()
#> pref_start pref_end suff_start suff_end #> "" " -> " " -> " ""
arrow_notation()
#> pref_start pref_end suff_start suff_end #> "" " -> " " -> " ""
bracket_notation()
#> pref_start pref_end suff_start suff_end #> "" " [" " [" "]"
split_pref_suff("a -> b", notation = arrow_notation())
#> $pref #> [1] "a" #> #> $suff #> [1] "b" #>
flip_pref_suff("a [b]", notation = bracket_notation())
#> [1] "b [a]"
keep_pref_suff("a -> b", keep = "suff", notation = arrow_notation())
#> [1] "b"
switch_notation("a -> b", from = arrow_notation(), to = bracket_notation())
#> [1] "a [b]"
switch_notation("a -> b", from = arrow_notation(), to = bracket_notation(), flip = TRUE)
#> [1] "b [a]"
m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = TRUE, dimnames = list(c("b [a]", "d [c]"), c("f [e]", "h [g]"))) %>% setrowtype("Products [Industries]") %>% setcoltype("Industries [Products]") m
#> f [e] h [g] #> b [a] 1 2 #> d [c] 3 4 #> attr(,"rowtype") #> [1] "Products [Industries]" #> attr(,"coltype") #> [1] "Industries [Products]"
switch_notation_byname(m, from = bracket_notation(), to = arrow_notation(), flip = TRUE)
#> e -> f g -> h #> a -> b 1 2 #> c -> d 3 4 #> attr(,"rowtype") #> [1] "Industries -> Products" #> attr(,"coltype") #> [1] "Products -> Industries"
# Also works for lists. # Note that margin must be specified as a list here. switch_notation_byname(list(m, m), margin = list(c(1, 2)), from = bracket_notation(), to = arrow_notation(), flip = TRUE)
#> [[1]] #> e -> f g -> h #> a -> b 1 2 #> c -> d 3 4 #> attr(,"rowtype") #> [1] "Industries -> Products" #> attr(,"coltype") #> [1] "Products -> Industries" #> #> [[2]] #> e -> f g -> h #> a -> b 1 2 #> c -> d 3 4 #> attr(,"rowtype") #> [1] "Industries -> Products" #> attr(,"coltype") #> [1] "Products -> Industries" #>