Consider the following example. Is that possible to delete stopwords
from text
?
library(tm)
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- ("≤µm", "≥°f", "±μgm")
你可以像下面这样尝试gsub
gsub(paste0(stopwords, collapse = "|"),"",text)
First, you have some errors in your example strings. text
is missing quotation marks and stopwords
is missing the c
before the brackets.
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")
You can remove the values in stopwords from your string using stringr as below:
library(stringr)
str_replace_all(text, paste(stopwords, collapse = "|"), "")
Since you are going to be doing text mining, you may be wanting to convert your input string into a vector of words. If so, you can easily remove the stopwords by subsetting.
library(stringr)
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")
text <- unlist(str_split(text, " "))
text[!(sapply(text, function (x) any(str_detect(stopwords, x))))]
If your work has you putting your words in a data.frame or similar, then there is another way:
library(dplyr)
library(stringr)
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")
text <- unlist(str_split(text, " "))
data.frame(words = text) %>% anti_join(data.frame(words = 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.