0

I have the following data, which consists of "Frequency" information that I extracted using a Python Script. I want to use this information to generate a WordCloud2 in R.

                  WORD VALUE SENT
1         topnotch     1    1
2             good     2    1
3             nice     11    0
4         inspired     14    0
5        beautiful     21    0

As I have already done the data pre-processing and cleaning in a previous step, I have no need to go through the steps that are being explained to me in all of the tutorials that I am reading, such as:

  conv2 <- Corpus(VectorSource(abstracts))

      text.corpus = tm_map(conv2, removePunctuation)
      text.corpus = tm_map(text.corpus, content_transformer(tolower))
      text.corpus = tm_map(text.corpus, removeWords, stopwords("english"))

However, I still need to convert my data into a proper matrix in order to index it correctly to generate a WordCloud, this indexing process typically consists of the following steps:

 tdm <- TermDocumentMatrix(text.corpus)
      m <- as.matrix(tdm)
      v <- sort(rowSums(m),decreasing=TRUE)
      d <- data.frame(word = names(v),freq=v)

I have been looking to find examples of what the format should be so that I can directly encode my frequency table into the tdm/df necessary for WordCloud2. Can anyone point me in the direction to the code, as directly calling my table into the TDM has not worked thus far, example:

tdm <- TermDocumentMatrix(mr)
m <- as.matrix(tdm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)

The errors that I am receiving are the following:

Error in UseMethod("TermDocumentMatrix", x) : 
  no applicable method for 'TermDocumentMatrix' applied to an object of class "data.frame"
> m <- as.matrix(tdm)
Error in as.matrix(tdm) : object 'tdm' not found
> v <- sort(rowSums(m),decreasing=TRUE)
> d <- data.frame(word = names(v),freq=v)

when trying, as suggested below to use as.TermDocumentMatrix, I get the following error:

> tdm <- as.TermDocumentMatrix(mr)
Error in .TermDocumentMatrix(x, weighting) : 
  argument "weighting" is missing, with no default 
owwoow14
  • 1,694
  • 8
  • 28
  • 43
  • 1
    Did you try `as.TermDocumentMatrix(mr)`? – ulfelder Oct 17 '16 at 16:37
  • When trying that, I received the following error: `Error in .TermDocumentMatrix(x, weighting) : argument "weighting" is missing, with no default` – owwoow14 Oct 17 '16 at 16:53
  • 1
    According to the wordcloud2 docs you need to supply the terms and their frequencies. There is no need for a TDM. Just remove the "Sent" column. https://cran.r-project.org/web/packages/wordcloud2/wordcloud2.pdf – emilliman5 Oct 18 '16 at 19:17
  • Yes, I see what you mean, but when trying to implement `wordcloud` with `shiny`, (for instance: http://shiny.rstudio.com/gallery/word-cloud.html) all of the tutorials and explanations that I am seeing look like the require a `TDM`, unless there is a workaround that I can directly use my input to generate a `shiny` object? – owwoow14 Oct 20 '16 at 09:33

0 Answers0