从R中的文本中删除单词和符号

从R中的文本中删除单词和符号,r,text,stringr,tm,R,Text,Stringr,Tm,考虑下面的例子。可以从文本中删除停止字吗 首先,示例字符串中有一些错误。文本缺少引号,停止字缺少括号前的c text <- c("this is exercise for text mining ≤µm ≥°f ±μgm") stopwords <- c("≤µm", "≥°f", "±μgm") 首先,示例字符串中有一些错误。文本缺少引号,停止字缺少括号前的c text <- c("t

考虑下面的例子。可以从文本中删除停止字吗


首先,示例字符串中有一些错误。文本缺少引号,停止字缺少括号前的c

text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")

首先,示例字符串中有一些错误。文本缺少引号,停止字缺少括号前的c

text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")
您可以像下面这样尝试gsub

gsub(paste0(stopwords, collapse = "|"),"",text)
您可以像下面这样尝试gsub

gsub(paste0(stopwords, collapse = "|"),"",text)

由于您将要进行文本挖掘,因此可能需要将输入字符串转换为单词向量。如果是这样,您可以通过子集轻松删除stopwords

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))))]
如果您的工作让您将文字放入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))

由于您将要进行文本挖掘,因此可能需要将输入字符串转换为单词向量。如果是这样,您可以通过子集轻松删除stopwords

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))))]
如果您的工作让您将文字放入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))