19

I have a character vector of stopwords in R:

stopwords = c("a" ,
            "able" ,
            "about" ,
            "above" ,
            "abst" ,
            "accordance" ,
            ...
            "yourself" ,
            "yourselves" ,
            "you've" ,
            "z" ,
            "zero")

Let's say I have the string:

str <- c("I have zero a accordance")

How can remove my defined stopwords from str?

I think gsub or another grep tool could be a good candidate to pull this off, although other recommendations are welcome.

zthomas.nc
  • 3,689
  • 8
  • 35
  • 49

3 Answers3

27

Try this:

str <- c("I have zero a accordance")

stopwords = c("a", "able", "about", "above", "abst", "accordance", "yourself",
"yourselves", "you've", "z", "zero")

x <- unlist(strsplit(str, " "))

x <- x[!x %in% stopwords]

paste(x, collapse = " ")

# [1] "I have"

Addition: Writing a "removeWords" function is simple so it is not necessary to load an external package for this purpose:

removeWords <- function(str, stopwords) {
  x <- unlist(strsplit(str, " "))
  paste(x[!x %in% stopwords], collapse = " ")
}

removeWords(str, stopwords)
# [1] "I have"
Mikko
  • 7,530
  • 8
  • 55
  • 92
  • 3
    I find this way better than the implemented function in the tm package, because the latter has size limits. I work with a corpus of forum comments and wanted to remove all the usernames from the text (around 70000). I kept getting an error from R, because the regex was too large. Thank you! – Anastasia Pupynina May 20 '17 at 15:29
  • 1
    This solution is way faster than the `tm`package! Thanks for sharing!! – Peter Aug 04 '19 at 15:42
  • Note that this is case-sensitive, so blacklisted words starting a sentence (for example) will not be removed. It also splits by spaces, so blacklisted words next to any punctuation won't be removed either. Whether this is a big deal or not depends on your purposes. For the first issue, either change line 2 to `x <- tolower(unlist(strsplit(str, " ")))` to make everything lower-case to match the stopword list (if preserving capitalisation is not important), or duplicate the stopword list to have a copy of each word starting with a capital letter (if capitalisation is important). – DuckPyjamas Mar 08 '23 at 19:34
21

You could use the tm library for this:

require("tm")
removeWords(str,stopwords)
#[1] "I have   "
RHertel
  • 23,412
  • 5
  • 38
  • 64
0

Here is another option for a function if you want the code to be vectorized for many sentences, not just one. It borrows content from Mikko's original answer.

remove_words <- function(str, words) {
      
  purrr::map_chr(
    str, 
    function(sentence) {
      sentence_split <- unlist(strsplit(sentence, " "))
      paste(sentence_split[!sentence_split %in% words], collapse = " ")
    }
  )
      
}
    
remove_words(c('Hello world', 'This is another sentence', 'Test sentence 3'), c('world', 'sentence'))
Harrison Jones
  • 2,256
  • 5
  • 27
  • 34