Description Usage Arguments Value See Also Examples
mgsub
- A wrapper for gsub
that takes a vector
of search terms and a vector or single value of replacements.
mgsub_fixed
- An alias for mgsub
.
mgsub_regex
- An wrapper for mgsub
with fixed = FALSE
.
mgsub_regex_safe
- An wrapper for mgsub
.
1 2 3 4 5 6 7 8 9 10 | mgsub(x, pattern, replacement, leadspace = FALSE, trailspace = FALSE,
fixed = TRUE, trim = FALSE, order.pattern = fixed, safe = FALSE, ...)
mgsub_fixed(x, pattern, replacement, leadspace = FALSE, trailspace = FALSE,
fixed = TRUE, trim = FALSE, order.pattern = fixed, safe = FALSE, ...)
mgsub_regex(x, pattern, replacement, leadspace = FALSE, trailspace = FALSE,
fixed = FALSE, trim = FALSE, order.pattern = fixed, ...)
mgsub_regex_safe(x, pattern, replacement, ...)
|
x |
A character vector. |
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. |
leadspace |
logical. If |
trailspace |
logical. If |
fixed |
logical. If |
trim |
logical. If |
order.pattern |
logical. If |
safe |
logical. If |
... |
Additional arguments passed to |
mgsub
- Returns a vector with the pattern replaced.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | mgsub(DATA$state, c("it's", "I'm"), c("it is", "I am"))
mgsub(DATA$state, "[[:punct:]]", "PUNC", fixed = FALSE)
## Not run:
library(textclean)
hunthou <- replace_number(seq_len(1e5))
textclean::mgsub(
"'twenty thousand three hundred five' into 20305",
hunthou,
seq_len(1e5)
)
## "'20305' into 20305"
## Larger example from: https://stackoverflow.com/q/18332463/1000343
## A slower approach
fivehunthou <- replace_number(seq_len(5e5))
testvect <- c("fifty seven", "four hundred fifty seven",
"six thousand four hundred fifty seven",
"forty six thousand four hundred fifty seven",
"forty six thousand four hundred fifty seven",
"three hundred forty six thousand four hundred fifty seven"
)
textclean::mgsub(testvect, fivehunthou, seq_len(5e5))
## Safe substitution: Uses the mgsub package as the backend
dubious_string <- "Dopazamine is a fake chemical"
pattern <- c("dopazamin","do.*ne")
replacement <- c("freakout","metazamine")
mgsub(dubious_string, pattern, replacement, ignore.case = TRUE, fixed = FALSE)
mgsub(dubious_string, pattern, replacement, safe = TRUE, fixed = FALSE)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.