简体   繁体   中英

Removing words featured in character vector from string

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.

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"

You could use the tm library for this:

require("tm")
removeWords(str,stopwords)
#[1] "I have   "

If stopwords is long, the removeWords() solution should be much faster than any regex based solution.

For completeness, in case str is a vector of strings, one can write:

library("magrittr")
library("stringr")
library("purrr")

remove_words <- function(x, .stopwords) {
  x %>%
    stringr::str_split(" ") %>%
    purrr::flatten_chr() %>%
    setdiff(.stopwords) %>%
    stringr::str_c(collapse = " ")
}
purrr::map_chr(str, remove_words, .stopwords = stopwords)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM