[英]Removing words and symbols from text in R
Consider the following example.考虑以下示例。 Is that possible to delete
stopwords
from text
?是否可以从
text
删除stopwords
?
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
缺少引号并且stopwords
缺少括号前的c
。
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:您可以使用 stringr 从字符串中删除停用词中的值,如下所示:
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:如果你的工作让你把你的话放在 data.frame 或类似的文件中,那么还有另一种方法:
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.