boomt
boomt

Reputation: 125

Match and replace multiple strings in a vector of text without looping in R

I am trying to apply gsub in R to replace a match in string a with the corresponding match in string b. For example:

a <- c("don't", "i'm", "he'd")
b <- c("do not", "i am", "he would")
c <- c("i'm going to the party", "he'd go too")
newc <- gsub(a, b, c)

with the desired result being

newc = c("i am going to the party", "he would go too")

This approach does not work because gsub only accepts a string of length 1 for a and b. Executing a loop to cycle through a and b will be very slow since the real a and b have a length of 90 and c has a length > 200,000. Is there a vectorized way in R to perform this operation?

Upvotes: 9

Views: 14410

Answers (4)

user22477356
user22477356

Reputation: 1

Use a recursive function. Something like this:

gsub.rec <- function(a, b, c) {
  if(length(a) == 0) c else gsub.rec(a[-1], b[-1], gsub(a[1], b[1], c, fixed = TRUE))
}

a <- c("don't", "i'm", "he'd")
b <- c("do not", "i am", "he would")
c <- c("i'm going to the party", "he'd go too")

gsub.rec(a, b, c)

#> [1] "i am going to the party" "he would go too"  

Upvotes: 0

mnist
mnist

Reputation: 6954

Another base R solution with a functional programming style.

#' Replace Multiple Strings in a Vector
#'
#' @param x vector with strings to replace
#' @param y vector with strings to use instead
#' @param vec initial character vector
#' @param ... arguments passed to `gsub`
replace_strngs <- function(x, y, vec, ...) {
  # iterate over strings
  vapply(X = vec, 
         FUN.VALUE = character(1),
         USE.NAMES = FALSE, 
         FUN = function(x_string) {
           # iterate over replacements
           Reduce(
             f = function(s, x) {
               gsub(pattern = x[1],
                    replacement = x[2],
                    x = s,
                    ...) 
             },
             x = Map(f = base::c, x, y),
             init = x_string)
         })
}

a <- c("don't", "i'm", "he'd")
b <- c("do not", "i am", "he would")
c <- c("i'm going to the party", "he'd go too")

replace_strngs(a, b, c, fixed = TRUE)
#> [1] "i am going to the party" "he would go too"

Upvotes: 0

sbha
sbha

Reputation: 10422

stringr::str_replace_all() is an option:

library(stringr)
names(b) <- a
str_replace_all(c, b)
[1] "i am going to the party" "he would go too"  

Here is the same code but with different labels to hopefully make it a little clearer:

to_replace <- a
replace_with <- b
target_text <- c

names(replace_with) <- to_replace
str_replace_all(target_text, replace_with)

Upvotes: 5

G. Grothendieck
G. Grothendieck

Reputation: 269824

1) gsubfn gsubfn in the gsubfn package is like gsub except the replacement string can be a character string, list, function or proto object. If its a list it will replace each matched string with the component of the list whose name equals the matched string.

library(gsubfn)
gsubfn("\\S+", setNames(as.list(b), a), c)

giving:

[1] "i am going to the party" "he would go too"    

2) gsub For a solution with no packages try this loop:

cc <- c
for(i in seq_along(a)) cc <- gsub(a[i], b[i], cc, fixed = TRUE)

giving:

> cc
[1] "i am going to the party" "he would go too"        

Upvotes: 17

Related Questions