TermDocumentMatrix有时会抛出错误

时间:2014-09-06 10:31:54

标签: r word-cloud term-document-matrix

我正在根据来自不同运动队的推文创建一个词云。此代码成功执行大约1次10次:

handle <- 'arsenal'
txt <- searchTwitter(handle,n=1000,lang='en')
t <- sapply(txt,function(x) x$getText())
t <- gsub('http.*\\s*|RT|Retweet','',t)
t <- gsub(handle,'',t)
t_c <- Corpus(VectorSource(t))
tdm = TermDocumentMatrix(t_c,control = list(removePunctuation = TRUE,stopwords = stopwords("english"),removeNumbers = TRUE, content_transformer(tolower)))
m = as.matrix(tdm)
word_freqs = sort(rowSums(m), decreasing=TRUE) 
dm = data.frame(word=names(word_freqs), freq=word_freqs)
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"),rot.per=0.5)

其他9次中有10次,它会抛出以下错误:

Error in simple_triplet_matrix(i = i, j = j, v = as.numeric(v), nrow = length(allTerms),  : 
  'i, j, v' different lengths
In addition: Warning messages:
1: In mclapply(unname(content(x)), termFreq, control) :
  all scheduled cores encountered errors in user code
2: In simple_triplet_matrix(i = i, j = j, v = as.numeric(v), nrow = length(allTerms),  :
  NAs introduced by coercion

任何想法的家伙?我用谷歌搜索了,但到目前为止已经很短​​了!请记住,我是R的绝对新手!

2 个答案:

答案 0 :(得分:5)

因此,经过一段时间的游戏后,以下代码行完全解决了我的问题:

t <- iconv(t,to="utf-8-mac")

答案 1 :(得分:0)

如果删除:

corpus = tm_map(corpus, PlainTextDocument)

你还必须删除:

t_c <- Corpus(VectorSource(t))

然后,您将获得TermDocumentMatrix的正确输出。