LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Sentiment Analysis in R

Kurs anzeigen

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

moby_tidy <- moby %>%
  # Inner join to nrc lexicon
  ___(___, by = c("term" = "word")) %>% 
  # Drop positive or negative
  ___(!___("___", sentiment)) %>% 
  # Count by sentiment and term
  ___(___, ___) %>% 
  # Pivot sentiment, using n for values
  ___(names_from = ___, values_from = ___, values_fill = ___) %>%
  # Convert to data.frame, making term the row names
  data.frame(row.names = "___")

# Examine
head(moby_tidy)
Code bearbeiten und ausführen