Wordcloud2 - separate words for counting

Viewed 26

am trying to extract the words so that I can create a wordcloud but have some difficulties this is the code:

library(readxl)
data <- read_excel("C:\\Users\\me\\OneDrive\\Desktop\\ToPandas.xlsx")

data2 <-data$articlesDescription


#install.packages("wordcloud2")
#install.packages("tidyverse")
#install.packages("tidytext")

library(wordcloud2)
library(tidyverse)
library(tidytext)

data2 <- gsub('[^[:alnum:] ]', '', data2)

data2 <-  data2 %>% 
  ungroup()

data3.df <- as.data.frame(data2)
data3 <- data3.df

data3 <- data3%>%
  anti_join(get_stopwords())%>%
  unnest_tokens(word, text) %>%
  count(word, sort = TRUE)

I have put the hash tags in front of the install packages so it does not try to reinstall. up to data2 until I start to ungroup then I get this error:

Error in UseMethod("ungroup") : no applicable method for 'ungroup' applied to an object of class "character"

then when it tries to move forward I get this:

Error in anti_join(): ! by must be supplied when x and y have no common variables. i use by = character()` to perform a cross-join.

I think that my error stems from the first error (ungroup) but I can't figure out how to do it so I can count the words

this is a sample of how the imported xlsx file looks like: ToPandas_xlsx Image

Can anyone point me into the right direction? thanks :)

1 Answers

Maybe this will be enough to get you started:

test <- data.frame(Text = rep("The quick brown fox jumped over the lazy dog's back.", 5))

Now split out the words:

test.lst <- strsplit(test$Text, " ")
test.lst[[1]]
#  [1] "The"    "quick"  "brown"  "fox"    "jumped" "over"   "the"    "lazy"   "dog's"  "back." 

Get rid of the punctuation:

test.lst2 <- lapply(test.lst, function(x) gsub("[[:punct:]]", "", x))
test.lst2[[1]]
#  [1] "The"    "quick"  "brown"  "fox"    "jumped" "over"   "the"    "lazy"   "dogs"   "back"  

test.lst2 is a list containing a part for each row of the data. If you want to collapse. To get frequencies:

table(unlist(test.lst2))

  back  brown   dogs    fox jumped   lazy   over  quick    the    The 
     5      5      5      5      5      5      5      5      5      5 
Related