使用 "TermDocumentMatrix" 时,没有适用于 'meta' 的方法应用于 class "character" 的对象

When using "TermDocumentMatrix", no applicable method for 'meta' applied to an object of class "character"

在我使用这个短语之前,"TermDocumentMatrix" 很好。

doc <- tm_map(doc, gsub, pattern = "buy", replacement = "bought")

但是,使用这个短语后,"TermDocumentMatrix"会产生错误。

Error in UseMethod("meta", x) : 
no applicable method for 'meta' applied to an object of class "character"

我需要一个单词替换。 所以我用了这句话。

我的文档结构如下。

1. so I bought it.
2. I bought the EH AC line in November 2014 
3. 3rd product bought from AC and all no good.
(skip)

如何使用"TermDocumentMatrix"?

library(tm)
library(XML)
library(SnowballC)

doc<-VCorpus(VectorSource(readLines(file.choose())))

doc <- tm_map(doc, stripWhitespace)

doc <- tm_map(doc, stemDocument)

doc<-tm_map(doc, content_transformer(tolower))

doc<-tm_map(doc, removeWords, stopwords("english"))

myStopwords <- c(stopwords("english"), "can", "will")
myStopwords <- setdiff(myStopwords, c("will","can"))
doc <- tm_map(doc, removeWords, myStopwords)

doc<-tm_map(doc,removeNumbers)


#If you omit this step, the error will not appear in "TermDocumentMatrix".
doc <- tm_map(doc, gsub, pattern = "buy", replacement = "bought")

doc <- TermDocumentMatrix(doc, control=list(removePunctuation=T))

您需要将适当的内容转换器传递给 tm_map,而不是任意字符操作函数

doc <- tm_map(doc, content_transformer(function(x) 
    gsub(x, pattern = "buy", replacement = "bought")))