在R

时间:2015-05-22 01:17:23

标签: r word-cloud

#Loading Packages

library(tm)
library(wordcloud)

library(RColorBrewer)

speech="/Users/GregDuncan/Desktop/bills.txt"
modi_txt = readLines(speech)


modi<-Corpus(VectorSource(modi_txt))


modi_data<-tm_map(modi,stripWhitespace)

modi_data<-tm_map(modi_data,tolower)

modi_data<-tm_map(modi_data,removeNumbers)

modi_data<-tm_map(modi_data,removePunctuation)

modi_data<-tm_map(modi_data,removeWords, stopwords(“english”))


tdm_modi<-TermDocumentMatrix (modi_data) #Creates a TDM

TDM1<-as.matrix(tdm_modi) #Convert this into a matrix format

v = sort(rowSums(TDM1), decreasing = TRUE) #Gives you the frequencies for every word

Summary(v)



wordcloud (modi_data, scale=c(5,0.5), max.words=1, random.order=FALSE, rot.per=0.35, use.r.layout=FALSE, colors=brewer.pal(8, “Dark2″))

所以我正试图从文档中创建一个词云。我试图用Rscript运行它,我遇到了一个问题,比如这个

  enter Error: unexpected input in "modi_data<-tm_map(modi_data,removeWords, stopwords(?"
Execution haltedcode here

   unexpected input in "wordcloud (modi_data, scale=c(5,0.5), max.words=1, random.order=FALSE, rot.per=0.35, use.r.layout=FALSE, colors=brewer.pal(8, �"

我对R很陌生,所以我想学习基础知识。我不知道有经验的人是否可以快速看到问题

1 个答案:

答案 0 :(得分:0)

请注意以下更改,并且应该运行。我使用了tm包中的数据集crude

library(tm)
library(wordcloud)

library(RColorBrewer)

data(crude)
modi_txt <- crude

modi<-Corpus(VectorSource(modi_txt))


modi_data<-tm_map(modi,stripWhitespace)

modi_data<-tm_map(modi_data,tolower)

modi_data<-tm_map(modi_data,removeNumbers)

modi_data<-tm_map(modi_data,removePunctuation)

modi_data<-tm_map(modi_data, removeWords, stopwords("english")) # NB quote marks were wrong.  This can happen if you copied from Word, for example

modi_data<-tm_map(modi_data, PlainTextDocument) # NB: added this call

tdm_modi<-TermDocumentMatrix (modi_data)

TDM1<-as.matrix(tdm_modi) 

v = sort(rowSums(TDM1), decreasing = TRUE) 

summary(v) # NB need lower case "s"

wordcloud (modi_data, scale=c(5,0.5), max.words=1, random.order=FALSE, rot.per=0.35,
           use.r.layout=FALSE, colors=brewer.pal(8, "Dark2"))  # NB had to fix quotes for "D...

这里的结果很奇怪,但您可能想要试验wordcloud个参数。

enter image description here