patband: Get first matching pattern band

Description Usage Arguments Value See Also Examples

Description

For each element in the string vector, this function finds the name (or number) of a collection of regex patterns — "band" — one of which was the first to match the string element. Order of bands in the bandlist influences the results. The function is useful when classifying character variables based on content.

Usage

1
patband(string, bandlist, as.factor = FALSE)

Arguments

string

vector of strings to match against patterns

bandlist

list of pattern bands: <name> = <vector of applicable patterns>

as.factor

if FALSE, return band names, not factors; otherwise if TRUE

Value

Vector of strings (or factors) of pattern "bands" that matched each string first, numeric pattern index if no band names given.

See Also

which_pattern() for the underlying pattern matching behaviour.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# In the following examples patterns should match as narrow as possible
# because the first match wins.
seasons <- list(
  Wi = c("De", "Ja", "Fe"),
  Sp = c("Ma", "Ap"),
  Su = c("Ju", "Au"),
  Au = c("Se", "Oc", "No")
)
patband(month.name, seasons, as.factor = TRUE)
# [1] Wi Wi Sp Sp Sp Su Su Su Au Au Au Wi
# Levels: Wi Sp Su Au

quarters <- list(
  c("Ja", "Fe", "Mar"),
  c("Ap", "May", "Jun"),
  c("Jul", "Au", "Se"),
  c("Oc", "No", "De")
  )
patband(month.name, quarters)
# [1] 1 1 1 2 2 2 3 3 3 4 4 4

avidclam/amxtra documentation built on May 17, 2019, 12:01 p.m.