Learn R Programming

kutils (version 1.73)

mgsub: apply a vector of replacements, one after the other.

Description

This is multi-gsub. Use it when it is necessary to process many patterns and replacements in a given order on a vector.

Usage

mgsub(pattern, replacement, x, ...)

Value

vector with pattern replaced by replacement

Arguments

pattern

vector of values to be replaced. A vector filled with patterns as documented in the gsub pattern argument

replacement

vector of replacements, otherwise same as gsub. Length of replacement must be either 1 or same as pattern, otherwise an error results.

x

the vector in which elements are to be replaced, same as gsub

...

Additional arguments to be passed to gsub

Author

Jared Harpole <jared.harpole@gmail.com> and Paul Johnson <pauljohn@ku.edu>

Examples

Run this code
x <- c("Tom", "Jerry", "Elmer", "Bugs")
pattern <- c("Tom", "Bugs")
replacement <- c("Thomas", "Bugs Bunny")
(y <- mgsub(pattern, replacement, x))
x[1] <- "tom"
(y <- mgsub(pattern, replacement, x, ignore.case = TRUE))
(y <- mgsub(c("Elmer", "Bugs"), c("Looney Characters"), x, ignore.case = TRUE))

Run the code above in your browser using DataLab