# removing words based on a predefined vector

I have the dataset test_stopword and I want to remove some words from the dataset based on a vector. How can I do that in R?

texts <- c("This is the first document.",
"Is this a text?",
"This is the second file.",
"This is the third text.",
"File is not this.")

test_stopword <- as.data.frame(texts)
ordinal_stopwords  <- c("first","primary","second","secondary","third")


texts <- c("This is the first document.",
"Is this a text?",
"This is the second file.",
"This is the third text.",
"File is not this.")

test_stopword <- as.data.frame(texts)
ordinal_stopwords  <- c("first","primary","second","secondary","third")

(newdata <- as.data.frame(sapply(texts, function(x)   gsub(paste(ordinal_stopwords, collapse = '|'), '', x))))


The output is getting skewed when added in a code block (maybe a bug in SE). But, you would get the desired output.

• Thank you. How can I do the replacement on the same dataset? – Hamideh Nov 27 '15 at 10:33
• You can simply replace newdata with test_stopword – Dawny33 Nov 27 '15 at 10:37
• It doesn't work. it creates a new column and puts the old column in row.names. – Hamideh Nov 27 '15 at 10:57
• @HamidehIraj Oh! Okay. You want to replace the existing one with the new ones, right? It's just a small tweak in that function :) Anyway, I'll edit the answer in a while. I'm travelling! – Dawny33 Nov 27 '15 at 10:59
• In addition, I have test_stopword\$texts not texts vector, so I am looking for something that works on the dataframe. – Hamideh Nov 27 '15 at 15:14