Learn R Programming

qdap (version 2.0.0)

multigsub: Multiple gsub

Description

multigsub - A wrapper for gsub that takes a vector of search terms and a vector or single value of replacements. sub_holder - This function holds the place for particular character values, allowing the user to manipulate the vector and then revert the place holders back to the original values.

Usage

multigsub(pattern, replacement = NULL, text.var, leadspace = FALSE,
  trailspace = FALSE, fixed = TRUE, trim = TRUE, order.pattern = fixed,
  ...)

mgsub(pattern, replacement = NULL, text.var, leadspace = FALSE,
  trailspace = FALSE, fixed = TRUE, trim = TRUE, order.pattern = fixed,
  ...)

sub_holder(pattern, text.var, ...)

Arguments

pattern
Character string to be matched in the given character vector.
replacement
Character string equal in length to pattern or of length one which are a replacement for matched pattern.
text.var
The text variable.
leadspace
logical. If TRUE inserts a leading space in the replacements.
trailspace
logical. If TRUE inserts a trailing space in the replacements.
fixed
logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.
trim
logical. If TRUE leading and trailing white spaces are removed.
order.pattern
logical. If TRUE and fixed = TRUE, the pattern string is sorted by number of characters to prevent substrings replacing meta strings (e.g., pattern = c("the", "then") resorts to search for "then" fir
...
Additional arguments passed to gsub.

Value

  • multigsub - Returns a vector with the pattern replaced. sub_holder - Returns a list with the following:
  • outputkeyed place holder character vector
  • unholdA function used to revert back to the original values

See Also

gsub

Examples

Run this code
multigsub(c("it's", "I'm"), c("it is", "I am"), DATA$state)
mgsub(c("it's", "I'm"), c("it is", "I am"), DATA$state)
mgsub("[[:punct:]]", "PUNC", DATA$state, fixed = FALSE)

## `sub_holder` Function
(fake_dat <- paste(emoticon[1:11,2], DATA$state))
(m <- sub_holder(emoticon[,2], fake_dat))
m$unhold(strip(m$output))
# With Stemming
m$unhold(stemmer(strip(m$output), capitalize = FALSE))

Run the code above in your browser using DataLab