chr.gsub: Multiple Pattern Matching And Replacements

View source: R/chr.gsub.R

chr.gsubR Documentation

Multiple Pattern Matching And Replacements

Description

This function is a multiple global string replacement wrapper that allows access to multiple methods of specifying matches and replacements.

Usage

chr.gsub(pattern, replacement, x, recycle = FALSE, ...)

Arguments

pattern

a character vector with character strings to be matched.

replacement

a character vector equal in length to pattern or of length one which are a replacement for matched patterns.

x

a character vector where matches and replacements are sought.

recycle

logical: if TRUE, replacement is recycled if lengths differ.

...

additional arguments to pass to the regexpr or sub function.

Value

Return a character vector of the same length and with the same attributes as x (after possible coercion to character).

Note

This function was adapted from the mgsub() function in the mgsub package by Mark Ewing (2019).

Author(s)

Mark Ewing

References

Mark Ewing (2019). mgsub: Safe, Multiple, Simultaneous String Substitution. R package version 1.7.1. https://CRAN.R-project.org/package=mgsub

See Also

chr.grep, chr.grepl, chr.omit, chr.trim

Examples

# Example 1: Replace 'the' and 'they' with 'a' and 'we'
chr.vector <- "they don't understand the value of what they seek."
chr.gsub(c("the", "they"), c("a", "we"), chr.vector)

# Example 2: Replace 'heyy' and 'ho' with 'yo'
chr.vector <- c("hey ho, let's go!")
chr.gsub(c("hey", "ho"), "yo", chr.vector, recycle = TRUE)

# Example 3: Replace with regular expressions
chr.vector <- "Dopazamine is not the same as dopachloride or dopastriamine, yet is still fake."
chr.gsub(c("[Dd]opa([^ ]*?mine)","fake"), c("Meta\1","real"), chr.vector)

misty documentation built on Oct. 24, 2024, 5:10 p.m.

Related to chr.gsub in misty...