str_replace_many {tidySummaries} | R Documentation |
Multiple Pattern-Replacement Substitutions
Description
Applies multiple regular expression substitutions to a character vector or a specific column of a data frame. Performs replacements sequentially
Usage
str_replace_many(x, pattern, replacement, column = NULL, ...)
Arguments
x |
A character vector or a data frame containing the text to modify. |
pattern |
A character vector of regular expressions to match. |
replacement |
A character vector of replacement strings, same length as 'pattern'. |
column |
Optional. If 'x' is a data frame, the name of the character column to apply the replacements to. |
... |
Additional arguments passed to 'gsub()', such as 'ignore.case = TRUE'. |
Value
- If 'x' is a character vector, returns a modified character vector. - If 'x' is a data frame, returns the data frame with the specified column modified.
Examples
# Example on a character vector
text <- c("The cat and the dog", "dog runs fast", "no animals")
str_replace_many(text, pattern = c("cat", "dog"), replacement = c("lion", "wolf"))
# Example on a data frame
library(tibble)
df <- tibble(id = 1:3, text = c("The cat sleeps", "dog runs fast", "no pets"))
str_replace_many(df, pattern = c("cat", "dog"), replacement = c("lion", "wolf"), column = "text")
[Package tidySummaries version 0.1.0 Index]