R Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
R Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
我正在使用以下代码创建文档术语矩阵。我在创建矩阵时没有问题,但是当我尝试删除稀疏项或查找频繁项时,出现错误。
text<- c("Since I love to travel, this is what I rely on every time.",
"I got this card for the no international transaction fee",
"I got this card mainly for the flight perks",
"Very good card, easy application process",
"The customer service is outstanding!")
library(tm)
corpus<- Corpus(VectorSource(text))
corpus<- tm_map(corpus, content_transformer(tolower))
corpus<- tm_map(corpus, removePunctuation)
corpus<- tm_map(corpus, removeWords, stopwords("english"))
corpus<- tm_map(corpus, stripWhitespace)
dtm<- as.matrix(DocumentTermMatrix(corpus))
结果如下:
Docs application card customer easy every ... etc.
1 0 0 0 1 0
2 0 1 0 0 1
3 0 1 0 0 0
4 1 1 0 0 0
5 0 0 1 0 0
这是我使用 removeSparseTerms 或 findFreqTerms
时出现错误的地方
sparse<- removeSparseTerms(dtm, 0.80)
freq<- findFreqTerms(dtm, 2)
结果
Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
removeSparseTerms and findFreqTerms 需要 DocumentTermMatrix 或 TermDocumentMatrix 对象而不是矩阵。
在不转换为矩阵的情况下创建 DocumentTermMatrix,您将不会收到错误。
dtm <- DocumentTermMatrix(corpus)
sparse <- removeSparseTerms(dtm, 0.80)
freq <- findFreqTerms(dtm, 2)
我正在使用以下代码创建文档术语矩阵。我在创建矩阵时没有问题,但是当我尝试删除稀疏项或查找频繁项时,出现错误。
text<- c("Since I love to travel, this is what I rely on every time.",
"I got this card for the no international transaction fee",
"I got this card mainly for the flight perks",
"Very good card, easy application process",
"The customer service is outstanding!")
library(tm)
corpus<- Corpus(VectorSource(text))
corpus<- tm_map(corpus, content_transformer(tolower))
corpus<- tm_map(corpus, removePunctuation)
corpus<- tm_map(corpus, removeWords, stopwords("english"))
corpus<- tm_map(corpus, stripWhitespace)
dtm<- as.matrix(DocumentTermMatrix(corpus))
结果如下:
Docs application card customer easy every ... etc.
1 0 0 0 1 0
2 0 1 0 0 1
3 0 1 0 0 0
4 1 1 0 0 0
5 0 0 1 0 0
这是我使用 removeSparseTerms 或 findFreqTerms
时出现错误的地方sparse<- removeSparseTerms(dtm, 0.80)
freq<- findFreqTerms(dtm, 2)
结果
Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
removeSparseTerms and findFreqTerms 需要 DocumentTermMatrix 或 TermDocumentMatrix 对象而不是矩阵。
在不转换为矩阵的情况下创建 DocumentTermMatrix,您将不会收到错误。
dtm <- DocumentTermMatrix(corpus)
sparse <- removeSparseTerms(dtm, 0.80)
freq <- findFreqTerms(dtm, 2)