朴素贝叶斯在R中,情感分析导致无法强制类错误

朴素贝叶斯在R中,情感分析导致无法强制类错误,r,sentiment-analysis,R,Sentiment Analysis,我有一个数据框df_tweets,它有两列tweets和score。 分数是一个值介于1到5之间的因子 getMatrix <- function(chrVect){ testsource <- VectorSource(chrVect) testcorpus <- Corpus(testsource) testcorpus <- tm_map(testcorpus,stripWhitespace) testcorpus <- tm_map(testcorp

我有一个数据框
df_tweets
,它有两列
tweets
score
。 分数是一个值介于
1到5之间的因子

getMatrix <- function(chrVect){
 testsource <- VectorSource(chrVect)
 testcorpus <- Corpus(testsource)
 testcorpus <- tm_map(testcorpus,stripWhitespace)
 testcorpus <- tm_map(testcorpus, removeWords, stopwords('french'))
 testcorpus <- tm_map(testcorpus, removeWords, stopwords('english'))
 testcorpus <- tm_map(testcorpus, content_transformer(tolower))
 testcorpus <- tm_map(testcorpus, removePunctuation)
 testcorpus <- tm_map(testcorpus, removeNumbers)
 testcorpus <- tm_map(testcorpus, PlainTextDocument)

 return(DocumentTermMatrix(testcorpus))
}

op =getMatrix(df_tweets$text)
classifier <-naiveBayes(as.matrix(op), as.factor(df_tweets$avg_score))

getMatrix我相信你可以用
as.data.frame
或者直接用
as.matrix.data.frame

as.matrix
包装成.matrix
或者直接用
as.matrix.data.frame
包装成.matrix
,这样做有效。如果你可以把它作为一个答案,我可以接受它/或删除我的帖子
myPrediction<- predict(classifier,op)

Error in as.data.frame.default(newdata) : 
cannot coerce class "c("DocumentTermMatrix", "simple_triplet_matrix")" to a data.frame