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

Exercise

Emotional introspection

In this exercise you go beyond subsetting on positive and negative language. Instead you will subset text by each of the 8 emotions in Plutchik's emotional wheel to construct a visual. With this approach you will get more clarity in word usage by mapping to a specific emotion instead of just positive or negative.

Using the tidytext subjectivity lexicon, "nrc", you perform an inner_join() with your text. The "nrc" lexicon has the 8 emotions plus positive and negative term classes. So you will have to drop positive and negative words after performing your inner_join(). One way to do so is with the negation, !, and grepl().

The "Global Regular Expression Print Logical" function, grepl(), will return a True or False if a string pattern is identified in each row. In this exercise you will search for positive OR negative using the | operator, representing "or" as shown below. Often this straight line is above the enter key on a keyboard. Since the ! negation precedes grepl(), the T or F is switched so the "positive|negative" is dropped instead of kept.

Object <- tibble %>%
  filter(!grepl("positive|negative", column_name))

Next you apply count() on the identified words along with pivot_wider() to get the data frame organized.

comparison.cloud() requires its input to have row names, so you'll have to convert it to a base-R data.frame, calling data.frame() with the row.names argument.

Instructions 1/2

undefined XP
    1
    2
  • inner_join() moby to nrc.
  • Using filter() with a negation (!) and grepl() search for "positive|negative". The column to search is called sentiment.
  • Use count() to count by sentiment and term.
  • Reshape the data frame with pivot_wider(), passing in names_from = sentiment, values_from = n, and values_fill = 0.
  • Convert to plain data frame with data.frame(), making the term column into rownames.
  • Examine moby_tidy using head().