Q:

Deleting lots of characters from a list in R

I have a list of characters with sentences. I have about 10000+ lines. I want to delete 1000+ words from it. So I have a character vector with the words to be deleted. I am using the approach as follows:
c<-gsub(pattern = wordsToBeDeleted,replacement = "",x = mainList)

This is using only the first word. How can I get this done?

A:

gsub only takes one pattern at at time, but You could combine it with Reduce
#sample data
sentences<-c(
    "Morbi in tempus metus, quis commodo eros",
    "Cum sociis natoque penatibus et magnis dis parturient montes",
    "Nulla diam quam, imperdiet vitae blandit eu",
    "Nullam nec pellentesque sapien, ac mollis mauris")

words<-c("quis","eros","diam","nec")

New we loop over all the words, removing them from the sentences
Reduce(function(a,b) gsub(b,"", a,fixed=T), words, sentences)

which gives us
[1] "Morbi in tempus metus,  commodo "                            
[2] "Cum sociis natoque penatibus et magnis dis parturient montes"
[3] "Nulla  quam, imperdiet vitae blandit eu"                     
[4] "Nullam  pellentesque sapien, ac mollis mauris" 

