4

I have a 1,500-row vector created from a Twitter search using the XML package. I have then converted it to a Corpus to be used with the tm package. I want to ultimately create a wordcloud with some (the most frequent) of those words, so I converted it to a TermDocumentMatrix to be able to find terms with a minimum frequency. I create the object "a", which is a list of those terms.

a <- findFreqTerms(mydata.dtm, 10)

The wordcloud package does not work on document matrices. So now, I want to filter the original vector to include only the words included in the "a" object (If I use the object itself, of course, I only have one instance of each of the frequent words).

Any suggestions are greatly appreciated.

DSM
  • 342,061
  • 65
  • 592
  • 494
Dino Fire
  • 419
  • 2
  • 6
  • 9
  • hi, welcome to SP. Can you please paste in a sample of your data. You can use `dput(myData)` If it is too large you can use `dput(head(myData, 10))` – Ricardo Saporta Mar 19 '13 at 15:08
  • Possible duplicate: http://stackoverflow.com/q/15506118/1036500 – Ben Mar 19 '13 at 18:09

1 Answers1

6

You can convert your tdm object into a matrix and work with that to get something that wordcloud can work with:

library(tm)
library(wordcloud)
# example data from the tm package
data(crude)
tdm <- TermDocumentMatrix(crude,
                      control = list(removePunctuation = TRUE,
                                     stopwords = TRUE))
v <- rowSums(as.matrix(tdm))
names(v) <- rownames(as.matrix(tdm))
v <- sort(v, decreasing=T)

Now with this you can filter out infrequent words with standard subsetting ([), or you can use the min.freq argument to wordcloud when you want to plot:

wordcloud(names(v), v, min.freq=10, scale=c(10,.3))

wordcloud

James
  • 65,548
  • 14
  • 155
  • 193