1. Learn
  2. /
  3. Courses
  4. /
  5. Sentiment Analysis in R

Connected

Exercise

Bing tidy polarity: Count & pivot the white whale

In this exercise you will apply another inner_join() using the "bing" lexicon.

Then you will manipulate the results with both count() from dplyr and pivot_wider() from tidyr to learn about the text.

The pivot_wider() function spreads data across multiple columns. In this case the sentiment and corresponding n values represent the frequency of positive or negative terms for each line. Using pivot_wider() changes the data so that each row now has positive and negative values, even if it is 0.

Instructions

100 XP

In this exercise, your R session has m_dick_tidy which contains the book Moby Dick and bing, containing the lexicon similar to the previous exercise.

  • Perform an inner_join() on m_dick_tidy and bing.
    • As before, join the "term" column in m_dick_tidy to the "word" column in the lexicon.
    • Call the new object moby_lex_words.
  • Create a column index, equal to as.numeric() applied to document. This occurs within mutate() in the tidyverse.
  • Create moby_count by forwarding moby_lex_words to count(), passing in sentiment, index.
  • Generate moby_wide by piping moby_count to pivot_wider() where names_from equals the sentimentcolumn, values_from equals the n column and values are filled in with values_fill = 0.
  • arrange is the next pipe used to order the rows by index values