row-col-notation: Row and column notation

row-col-notationR Documentation

Row and column notation

Description

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.

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

  • 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]".

  • get_pref_suff() Selects only prefix or suffix, discarding notational elements and the rejected part. Internally, calls split_pref_suff() and selects only the suff portions.

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

  • 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.

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.

Usage

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

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

split_pref_suff(x, notation = RCLabels::arrow_notation, transpose = FALSE)

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

flip_pref_suff(x, notation = RCLabels::arrow_notation)

get_pref_suff(
  x,
  which = c("pref", "suff"),
  notation = RCLabels::arrow_notation
)

switch_notation(x, 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 vector 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.

transpose

A boolean that tells whether to purr::transpose() the result. Set transpose = TRUE when using split_pref_suff() in a dplyr::mutate() call in the context of a data frame. Default is FALSE.

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.

which

Tells which to keep, the prefix ("pref") or the suffix ("suff").

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.

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.

Examples

notation_vec()
arrow_notation
bracket_notation
split_pref_suff("a -> b", notation = arrow_notation)
split_pref_suff(c("a -> b", "c -> d", "e -> f"), notation = arrow_notation)
split_pref_suff(c("a -> b", "c -> d", "e -> f"), notation = arrow_notation,
                transpose = TRUE)
flip_pref_suff("a [b]", notation = bracket_notation)
get_pref_suff("a -> b", which = "suff", notation = arrow_notation)
switch_notation("a -> b", from = arrow_notation, to = bracket_notation)
switch_notation("a -> b", from = arrow_notation, to = bracket_notation,
                flip = TRUE)
# Also works for vectors
switch_notation(c("a -> b", "c -> d"), from = arrow_notation, to = bracket_notation)

RCLabels documentation built on March 18, 2022, 7:19 p.m.