Get startedGet started for free

Stressed Out!

Here you will adjust the negative words to account for the specific text. You will then compare the basic and custom polarity() scores.

A popular song from Twenty One Pilots is called "Stressed Out". If you scan the song lyrics, you will observe the song is about youthful nostalgia. Overall, most people would say the polarity is negative. Repeatedly the lyrics mention stress, fears and pretending.

Let's compare the song lyrics using the default subjectivity lexicon and also a custom one.

To start, you need to verify the key.pol subjectivity lexicon does not already have the term you want to add. One way to check is with grep(). The grep() function returns the row containing characters that match a search pattern. Here is an example used while indexing.

data_frame[grep("search_pattern", data_frame$column), ]

After verifying the slang or new word is not already in the key.pol lexicon you need to add it. The code below uses sentiment_frame() to construct the new lexicon. Within the code sentiment_frame() accepts the original positive word vector, positive.words. Next, the original negative.words are concatenated to "smh" and "kappa", both considered negative slang. Although you can declare the positive and negative weights, the default is 1 and -1 so they are not included below.

custom_pol <- sentiment_frame(positive.words, c(negative.words, "hate", "pain"))

Now you are ready to apply polarity and it will reference the custom subjectivity lexicon!

This exercise is part of the course

Sentiment Analysis in R

View Course

Exercise instructions

We've created stressed_out which contains the lyrics to the song "Stressed Out", by Twenty One Pilots.

  • Use polarity() on stressed_out to see the default score.
  • Check key.pol for any words containing "stress". Use grep() to index the data frame by searching in the x column.
  • Create custom_pol as a new sentiment data frame.
  • Reapply polarity() to stressed_out with the additional parameter polarity.frame = custom_pol to compare how the new words change the score to a more accurate representation of the song.

Hands-on interactive exercise

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

# stressed_out has been pre-defined
head(stressed_out)

# Basic lexicon score
___(___)

# Check the subjectivity lexicon
___[___("stress", x)]

# New lexicon
custom_pol <- ___(___, c(negative.words, "___", "___"))

# Compare new score
___(___, polarity.frame = ___)
Edit and Run Code