Removing words featured in character vector from string

前端 未结 2 976
傲寒
傲寒 2020-12-31 09:28

I have a character vector of stopwords in R:

stopwords = c(\"a\" ,
            \"able\" ,
            \"about\" ,
            \"above\" ,
            \"abst         


        
相关标签:
2条回答
  • 2020-12-31 10:01

    You could use the tm library for this:

    require("tm")
    removeWords(str,stopwords)
    #[1] "I have   "
    
    0 讨论(0)
  • 2020-12-31 10:19

    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"
    
    0 讨论(0)
提交回复
热议问题