Get startedGet started for free

Jump right in! Visualize polarity

Sentiment analysis helps you extract an author's feelings towards a subject. This exercise will give you a taste of what's to come!

We created text_df representing a conversation with person and text columns.

Use qdap's polarity() function to score text_df. polarity() will accept a single character object or data frame with a grouping variable to calculate a positive or negative score.

In this example you will use the magrittr package's dollar pipe operator %$%. The dollar sign forwards the data frame into polarity() and you declare a text column name or the text column and a grouping variable without quotes.

text_data_frame %$% polarity(text_column_name)

To create an object with the dollar sign operator:

polarity_object <- text_data_frame %$% 
  polarity(text_column_name, grouping_column_name)

More specifically, to make a quantitative judgement about the sentiment of some text, you need to give it a score. A simple method is a positive or negative value related to a sentence, passage or a collection of documents called a corpus. Scoring with positive or negative values only is called "polarity." A useful function for extracting polarity scores is counts() applied to the polarity object. For a quick visual call plot() on the polarity() outcome.

This exercise is part of the course

Sentiment Analysis in R

View Course

Exercise instructions

  • Examine the text_df conversation data frame.
  • Using %$% pass text_df to polarity() along with the column name text without quotes. This will print the polarity for all text.
  • Create a new object datacamp_conversation by forwarding text_df with %$% to polarity(). Pass in text followed by the grouping person column. This will calculate polarity according to each individual person. Since it is all within parentheses the result will be printed too.
  • Apply counts() to datacamp_conversation to print the specific emotional words that were found.
  • plot() the datacamp_conversation.

Hands-on interactive exercise

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

# Examine the text data
text_df

# Calc overall polarity score
text_df %$% polarity(___)

# Calc polarity score by person
(datacamp_conversation <- text_df %$% ___(___, ___))

# Counts table from datacamp_conversation
___(___)

# Plot the conversation polarity
___(___)
Edit and Run Code