Pattern replacement with multiple patterns
Arguments
- pattern
character
vector of patterns- replacement
character
vector of replacements- x
character
vector with input data to be curated- ignore.case
logical
indicating whether to perform pattern matching in case-insensitive manner, whereignore.case=TRUE
will ignore the uppercase/lowercase distinction.- replaceMultiple
logical
vector indicating whether to perform global substitution, wherereplaceMultiple=FALSE
will only replace the first occurrence of the pattern, usingbase::sub()
. Note that this vector can refer to individual entries inpattern
.- ...
additional arguments are passed to
base::gsub()
orbase::sub()
.
Details
This function is a simple wrapper around base::gsub()
when considering a series of pattern-replacement
combinations. It applies each pattern match and replacement
in order and is therefore not vectorized.
When x
input is a list
each vector in the list
is processed,
somewhat differently than processing one vector.
When the
list
contains anotherlist
, or whenlength(x) < 100
, each value inx
is iterated callinggsubs()
. This process is the slowest option, however not noticeble untilx
has length over 10,000.When the
list
does not contain anotherlist
and all values are non-factor, or all values arefactor
, they are unlisted, processed as a vector, then relisted. This process is nearly the same speed as processing one single vector, except the time it takes to confirm the list element classes.When values contain a mix of non-factor and
factor
values, they are separately unlisted, processed bygsubs()
, then relisted and combined afterward. Again, this process is only slightly slower than option 2 above, given that it callsgsubs()
twice, with two vectors.Note that
factor
values at input are replaced withcharacter
values at output, consistent withgsub()
.
See also
Other jam string functions:
asSize()
,
breaksByVector()
,
fillBlanks()
,
formatInt()
,
gsubOrdered()
,
makeNames()
,
nameVector()
,
nameVectorN()
,
padInteger()
,
padString()
,
pasteByRow()
,
pasteByRowOrdered()
,
sizeAsNum()
,
tcount()
,
ucfirst()