Replace string in R with patterns and replacements both vectors

Viewed 1000

Let's say I have two vectors like so:

a <- c("this", "is", "test")
b <- c("that", "was", "boy")

I also have a string variable like so:

string <- "this is a story about a test"

I want to replace values in string so that it becomes the following:

string <- "that was a story about a boy"

I could do this using a for loop but I want this to be vectorized. How should I do this?

5 Answers
Related