2017-10-29 27 views
0

다음 코드를 사용하여 문서 용어 행렬을 만듭니다. 행렬을 만드는 데 문제가 없지만 스파 스 조건을 제거하거나 잦은 조건을 찾으려고하면 오류가 발생합니다. 나도 removeSparseTerms 또는 findFreqTermsR 오류 : 상속 (x, c ("DocumentTermMatrix", "TermDocumentMatrix"))이 TRUE가 아닙니다.

sparse<- removeSparseTerms(dtm, 0.80) 
freq<- findFreqTerms(dtm, 2) 

결과

Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE 
를 사용하여 오류가 어디

다음
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 

은 다음과 같습니다

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)) 

은 결과입니다

답변

4

removeSparseTermsfindFreqTermsDocumentTermMatrix 또는 TermDocumentMatrix 대상이 아닌 행렬을 기대하고있다.

행렬로 변환하지 않고 DocumentTermMatrix를 작성하면 오류가 발생하지 않습니다.

dtm <- DocumentTermMatrix(corpus) 
sparse <- removeSparseTerms(dtm, 0.80) 
freq <- findFreqTerms(dtm, 2) 
+0

고마워요, 그 작품 ..하지만 어떻게 다시 매트릭스 형식으로 변환 할 생각합니까? 나는 asmatrix (DocumentTermMatrix (sparse))와 같은 드문 드문 주파수를 사용하려고 시도했지만 작동하지 않았다. – djacobs1216

+0

죄송합니다. 알아 냈습니다. 방금 as.matrix (희소)를했는데 효과가있었습니다. 다시 한 번 감사드립니다! – djacobs1216