Get startedGet started for free

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.

This exercise is part of the course

Sentiment Analysis in R

View Course

Exercise instructions

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Inner join
moby_lex_words <- inner_join(___, ___, by = c("___" = "___"))

moby_lex_words <- moby_lex_words %>%
  # Set index to numeric document
  mutate(___ = as.numeric(___))

moby_count <- moby_lex_words %>%
  # Count by sentiment, index
  ___(___, ___)

# Examine the counts
moby_count

moby_wide <- moby_count %>%
  # Pivot the sentiments
  pivot_wider(names_from = ___, values_from = ___, values_fill = ___) %>% 
  arrange(index)

# Review the pivoted data
moby_wide
Edit and Run Code