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

Exercise

Unhappy ending? Chronological polarity

Sometimes you want to track sentiment over time. For example, during an ad campaign you could track brand sentiment to see the campaign's effect. You saw a few examples of this at the end of the last chapter.

In this exercise you'll recap the workflow for exploring sentiment over time using the novel Moby Dick. One should expect that happy moments in the book would have more positive words than negative. Conversely dark moments and sad endings should use more negative language. You'll also see some tricks to make your sentiment time series more visually appealing.

Recall that the workflow is:

  1. Inner join the text to the lexicon by word.
  2. Count the sentiments by line.
  3. Reshape the data so each sentiment has its own column.
  4. (Depending upon the lexicon) Calculate the polarity as positive score minus negative score.
  5. Plot the polarity time series.

This exercise should look familiar: it extends Bing tidy polarity: Call me Ishmael (with ggplot2)!.

Instructions 1/2

undefined XP
    1
    2
  • inner_join() the pre-loaded tidy version of Moby Dick, moby, to the bing lexicon.
    • Join by the "term" column in the text and the "word" column in the lexicon.
  • Count by sentiment and index.
  • Reshape so that each sentiment has its own column using pivot_wider() with:
    • names_from = sentiment referring to the sentiment column
    • values_from = n gaining values from n column
    • values_fill = 0 to fill in NA as 0
  • Using mutate() add two columns: polarity and line_number.
    • Set polarity equal to the positive score minus the negative score.
    • Set line_number equal to the row number using the row_number() function.