order_substr: Order substrings of a character vector

Description Usage Arguments Details Value Note Examples

View source: R/order_substr.R

Description

Split a character vector, reorder the substrings, and paste back together.

Usage

1
2
3
4
5
6
7
order_substr(
  x,
  split = "\\s*,\\s*|\\s*/\\s*|\\s*-\\s*|\\s+",
  perl = TRUE,
  collapse = " ",
  reverse = FALSE
)

Arguments

x

a character vector.

split

a regular expressin by which to split. See Details.

perl

logical, whether to interpret split as a Perl-style regular expression.

collapse

a character string with which to collapse the re-ordered sub-strings.

reverse

logical, whether to reverse the ordering.

Details

By default, order_substr() splits each string of a character vector at any commas, forward slashes, hyphens, and whitespace, regardless of whether these characters are surrounded by whitespace.

Alternative regular expressions for splitting can be used via split. Other strings may be used to separate the alphabetized output via collapse.

Value

A character vector with substrings ordered alphabetically and re-collapsed.

Note

Cleaning with order_substr() works poorly when substrings are inconsistently separated by the split pattern. For example, the vector of tuna species below hyphenates yellow-fin but leaves bluefin as a single word. Therefore, with the default split patterns, order_substr() splits yellow-fin prior to alphabetization.

Examples

1
2
3
4
5
tuna <- c("tuna,skipjack", "tuna  , bluefin", "yellow-fin - tuna", "tuna,   albacore")
order_substr(tuna)

colors <- c("green/red", "yellow / blue", "orange purple")
order_substr(colors, collapse = "-", reverse = TRUE)

coletl/coler documentation built on May 12, 2021, 9:44 p.m.