1
$\begingroup$

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")
$\endgroup$
2
$\begingroup$
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.

$\endgroup$
  • $\begingroup$ Thank you. How can I do the replacement on the same dataset? $\endgroup$ – Hamideh Nov 27 '15 at 10:33
  • $\begingroup$ You can simply replace newdata with test_stopword $\endgroup$ – Dawny33 Nov 27 '15 at 10:37
  • $\begingroup$ It doesn't work. it creates a new column and puts the old column in row.names. $\endgroup$ – Hamideh Nov 27 '15 at 10:57
  • $\begingroup$ @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! $\endgroup$ – Dawny33 Nov 27 '15 at 10:59
  • $\begingroup$ In addition, I have test_stopword$texts not texts vector, so I am looking for something that works on the dataframe. $\endgroup$ – Hamideh Nov 27 '15 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.