我的数据框df_tweets
有两列tweets
和score
。
分数是值1 to 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))
当我使用预测功能时,我收到错误
myPrediction<- predict(classifier,op)
Error in as.data.frame.default(newdata) :
cannot coerce class "c("DocumentTermMatrix", "simple_triplet_matrix")" to a data.frame
我该如何解决这个问题?
答案 0 :(得分:1)
我相信你可以用as.matrix
或as.data.frame
直接包裹as.matrix.data.frame
。