Replace multiple keywords within strings from my own data.

Hi everyone!

I'm having a problem similar to this one, but in my case, I don't want to replace the keywords naming every one of them. I have a larger dataset to work with and I would like to replace the words using the data frame column (let's assume worng_words and new_words are columns of a data frame)
An illustrative example:

text <- c("just writing an illustrative text for example,",
          "but it has some different text in each sentence,",
          "so I am just gonna replicate it in here.")

wrong_words <-  c("illustrative text", "it in here", "for example")
new_words <- c("illustrative_text", "it_in_here", "for_example")

Now, I want to replace all the wrong_words for the new_words, but without naming them, just pulling them from data frame. Kind of:

str_replace_all(text, wrong_words, new_words)

or

gsub(text, wrong_words, new_words)

I know the functions above won't work that way, but it illustrates what I intend to.

Thanks for the help!

1 Like

Hi @amrodstef, if you have a few wrong and new_words, this is a solution:

library(tidyverse)
library(stringr)
text %>%
  str_replace_all(c("illustrative text" = "illustrative_text", 
                    "it in here" = "it_in_here", 
                    "for example" = "for_example"))


# [1] "just writing an illustrative_text for_example,"  
# [2] "but it has some different text in each sentence,"
# [3] "so I am just gonna replicate it_in_here." 

Thanks for the answer, @M_AcostaCH , but I have a lot of wrong and new words.

I did some tests with a few new and wrong words in the same way you're suggesting, but it's not what I was looking for. I'm looking for something that does this exact same thing, but with the names coming from a data frame column, for example - and without naming all the replacements.

I bet there is a package with a function that does this. Here is a solution with a simple loop.

library(stringr)
text <- c("just writing an illustrative text for example,",
           "but it has some different text in each sentence,",
           "so I am just gonna replicate it in here.")
 
wrong_words <-  c("illustrative text", "it in here", "for example")
new_words <- c("illustrative_text", "it_in_here", "for_example")
for (i in seq_along(wrong_words)) {
   text <- str_replace_all(text, wrong_words[i], new_words[i])
 }
text
[1] "just writing an illustrative_text for_example,"   "but it has some different text in each sentence,"
[3] "so I am just gonna replicate it_in_here."
2 Likes

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.