R/prepare_for_word_cloud.R

Defines functions prepare_for_word_cloud

Documented in prepare_for_word_cloud

#' Prepare for word cloud
#'
#' Prepare a character vector for use with wordcloud2
#' @param x A character vector 
#' @import dplyr
#' @export

prepare_for_word_cloud <- function(x){
  # Clean x
  x <- tolower(x)
  x <- gsub("([.-])|[[:punct:]]", "", x)
  x <- x[!is.na(x)]
  # Parse x
  x_parsed <- strsplit(x, ' ')
  # unlist
  x <- unlist(x_parsed)
  # Remove a, for, the
  x <- x[! x %in% c('a', 'for', 'the', 'and')]
  # Make dataframe
  x <- data_frame(word = x)
  # Get counts
  x <- x %>%
    group_by(word) %>%
    summarise(freq = n()) %>%
    arrange(desc(freq))
  x <- data.frame(x)
  row.names(x) <- x$word
  return(x)
}
databrew/databrew documentation built on Feb. 1, 2020, 2:28 p.m.