Row and column notation
row-col-notation.RdIt is often convenient to represent matrix 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 to generate specialized versions or otherwise manipulate row and column names on their own or as row or column names.
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, this function callssplit_pref_suff()and selects only the desired portion.notation_vec()Builds a vector of notation symbols in a standard format. By default, it builds a list of notation symbols that provides an arrow separator (" -> ") between prefix and suffix.paste_pref_suff()paste0's prefixes and suffixes, the inverse ofsplit_pref_suff(). Always returns a character vector.preposition_notation()Builds a list of notation symbols that provides (by default) square brackets around the suffix with a preposition ("prefix [preposition suffix]").split_pref_suff()Splits prefixes from suffixes, returning each in a list with namesprefandsuff. If no prefix or suffix delimiters are found,xis returned in theprefitem, unmodified, and thesuffitem is returned as""(an empty string). If there is no prefix, and empty string is returned for theprefitem. If there is no suffix, and empty string is returned for thesuffitem.switch_notation()Switches from one type of notation to another based on thefromandtoarguments. Optionally, prefix and suffix can beflipped.
Parts of a notation vector are
"pref_start", "pref_end", "suff_start", and "suff_end".
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.
If sep only is specified for notation_vec() (default is " -> "),
pref_start, pref_end, suff_start, and suff_end are
set appropriately.
For functions where the notation argument is used to identify portions of the row or column label
(such as split_pref_suff(), get_pref_suff(),
and the from argument to switch_notation()),
(Note: flip_pref_suff() cannot infer notation, because it switches prefix and suffix in a known, single notation.)
if notation is a list, it is treated as a store from which
the most appropriate notation is inferred by infer_notation(choose_most_specific = TRUE).
Because default is RCLabels::notations_list,
notation is inferred by default.
The argument choose_most_specific tells what to do when two notations match a label:
if TRUE (the default), the notation with most characters is selected.
If FALSE, the first matching notation in notation will be selected.
See details at infer_notation().
If specifying more than one notation, be sure the notations are in a list.
notation = c(RCLabels::bracket_notation, RCLabels::arrow_notation)
is unlikely to produce the desired result, because the notations
are concatenated together to form a long string vector.
Rather say
notation = list(RCLabels::bracket_notation, RCLabels::arrow_notation).
For functions that construct labels (such as paste_pref_suff()),
notation can be a list of notations
over which the paste tasks is mapped.
If notation is a list, it must have as many items as
there are prefix/suffix pairs to be pasted.
If either pref or suff are a zero-length character vector
(essentially an empty character vector
such as obtained from character())
input to paste_pref_suff(),
an error is thrown.
Instead, use an empty character string
(such as obtained from "").
Usage
notation_vec(
sep = " -> ",
pref_start = "",
pref_end = "",
suff_start = "",
suff_end = ""
)
preposition_notation(preposition, suff_start = " [", suff_end = "]")
split_pref_suff(
x,
transpose = FALSE,
inf_notation = TRUE,
notation = RCLabels::notations_list,
choose_most_specific = TRUE
)
paste_pref_suff(
ps = list(pref = pref, suff = suff),
pref = NULL,
suff = NULL,
notation = RCLabels::arrow_notation,
squish = TRUE
)
flip_pref_suff(
x,
notation = RCLabels::notations_list,
inf_notation = TRUE,
choose_most_specific = TRUE
)
get_pref_suff(
x,
which = c("pref", "suff"),
inf_notation = TRUE,
notation = RCLabels::notations_list,
choose_most_specific = TRUE
)
switch_notation(
x,
from = RCLabels::notations_list,
to,
flip = FALSE,
inf_notation = TRUE
)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.
- transpose
A boolean that tells whether to
purr::transpose()the result. Settranspose = TRUEwhen usingsplit_pref_suff()in adplyr::mutate()call in the context of a data frame. Default isFALSE.- inf_notation
A boolean that tells whether to infer notation for
x. Default isTRUE. Seeinfer_notation()for details.- notation
A notation vector generated by one of the
*_notation()functions, such asnotation_vec(),arrow_notation, orbracket_notation.- choose_most_specific
A boolean that tells whether to choose the most specific notation from the
notationargument when thenotationargument is a list.- ps
A list of prefixes and suffixes in which each item of the list is itself a list with two items named
prefandsuff.- 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.- squish
A boolean that tells whether to remove extra spaces in the output of
paste_*()functions. Default isTRUE.- which
Tells which to keep, the prefix ("pref") or the suffix ("suff").
- from
The
notationto switch away from.- to
The
notationto 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()
#> 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"
#>
# Or infer the notation (by default from notations_list)
split_pref_suff("a -> b")
#> $pref
#> [1] "a"
#>
#> $suff
#> [1] "b"
#>
split_pref_suff(c("a -> b", "c -> d", "e -> f"))
#> $pref
#> [1] "a" "c" "e"
#>
#> $suff
#> [1] "b" "d" "f"
#>
split_pref_suff(c("a -> b", "c -> d", "e -> f"), transpose = TRUE)
#> [[1]]
#> [[1]]$pref
#> [1] "a"
#>
#> [[1]]$suff
#> [1] "b"
#>
#>
#> [[2]]
#> [[2]]$pref
#> [1] "c"
#>
#> [[2]]$suff
#> [1] "d"
#>
#>
#> [[3]]
#> [[3]]$pref
#> [1] "e"
#>
#> [[3]]$suff
#> [1] "f"
#>
#>
flip_pref_suff("a [b]", notation = bracket_notation)
#> [1] "b [a]"
# Infer notation
flip_pref_suff("a [b]")
#> [1] "b [a]"
get_pref_suff("a -> b", which = "suff")
#> suff
#> "b"
switch_notation("a -> b", from = arrow_notation, to = bracket_notation)
#> [1] "a [b]"
# Infer notation and flip prefix and suffix
switch_notation("a -> b", to = bracket_notation, flip = TRUE)
#> [1] "b [a]"
# Also works for vectors
switch_notation(c("a -> b", "c -> d"),
from = arrow_notation,
to = bracket_notation)
#> [[1]]
#> [1] "a [b]"
#>
#> [[2]]
#> [1] "c [d]"
#>
# Functions can infer the correct notation and return multiple matches
infer_notation("a [to b]",
allow_multiple = TRUE,
choose_most_specific = FALSE)
#> [[1]]
#> pref_start pref_end suff_start suff_end
#> "" " [" " [" "]"
#>
#> [[2]]
#> pref_start pref_end suff_start suff_end
#> "" " [to " " [to " "]"
#>
# Or choose the most specific notation
infer_notation("a [to b]",
allow_multiple = TRUE,
choose_most_specific = TRUE)
#> pref_start pref_end suff_start suff_end
#> "" " [to " " [to " "]"
# When setting the from notation, only that type of notation will be switched
switch_notation(c("a -> b", "c [to d]"),
from = arrow_notation,
to = bracket_notation)
#> [[1]]
#> [1] "a [b]"
#>
#> [[2]]
#> [1] "c [to d]"
#>
# But if notations are inferred, all notations can be switched
switch_notation(c("a -> b", "c [to d]"), to = bracket_notation)
#> [[1]]
#> [1] "a [b]"
#>
#> [[2]]
#> [1] "c [d]"
#>
# A double-switch can be accomplished.
# In this first example, `RCLabels::first_dot_notation` is inferred.
switch_notation("a.b.c", to = arrow_notation)
#> [1] "a -> b.c"
# In this second example,
# it is easier to specify the `from` and `to` notations.
switch_notation("a.b.c", to = arrow_notation) %>%
switch_notation(from = first_dot_notation, to = arrow_notation)
#> [1] "a -> b -> c"
# "" can be used as an input
paste_pref_suff(pref = "a", suff = "", notation = RCLabels::from_notation)
#> [1] "a [from ]"