1

I have a dataframe with list of sentences in one column. Now i have a list of words and another list of replacement words as follows.

DF

Date       Sentences
9-Nov-16   nah, i got rid of them overyears ago i was only watching pbs by then anyway i have xfinity on demand+netflix on my 'puter
9-Nov-16   Omg Netflix is working on my Xfinity!

and so on

List of Words

words <- c("nah","'puter","Omg")
trans <- c("No","computer","Oh my God")

and so on.

Now i want to replace "Nah" with "No" , "'puter" with "computer", "Omg" with "Oh my God" and so on in the sentences present in the above dataframe. To achieve this i use the following code.

DF$Sentences<- str_replace_all( DF$Sentences, paste0("\\b",words,"\\b"), trans)

But this is not replacing the words in the sentence. Can someone tell me the correct way of doing this?

Venu
  • 239
  • 4
  • 18

2 Answers2

3

We can use mgsub from qdap

library(qdap)
DF$Sentences <- mgsub(words, trans, DF$Sentences)

Or in a for loop with gsub

for(j in seq_along(words)){
  DF$Sentences <- gsub(words[j], trans[j], DF$Sentences)
}
akrun
  • 874,273
  • 37
  • 540
  • 662
2

You can use stri_replace_all_fixed or the like from stringi with vectorize_all=FALSE, eg

stri_replace_all_fixed(DF[["Sentences"]], words,  trans, vectorize_all=FALSE)
Rorschach
  • 31,301
  • 5
  • 78
  • 129