How to Perform Sentiment Analysis in R

How to Perform Sentiment Analysis in R

Sentiment analysis is a text analysis technique that allows companies to make sense of qualitative data. By detecting positive and negative sentiment in text data, such as tweets, product reviews, and support tickets, you can understand how customers feel about your brand, product, or service, and gain insights that lead to data-driven decisions.

In this short guide, we’ll introduce you to an R package, created by data scientists Maëlle Salmon and Amanda Dobbyn, which works seamlessly with MonkeyLearn’s API and makes it easy to start performing sentiment analysis in R.

MonkeyLearn is a machine learning platform that makes it easy to build text classifiers for detecting sentiment, intent, topic, urgency and more. You can also build text extractors to identify specific data within text, including keywords, entities, features, and more.

Now that the introductions are out of the way, let’s get started!

Tutorial: How to Perform Sentiment Analysis in R

Amanda and Maëlle are evangelists within the R community, making noteworthy contributions on a regular basis. To make it easy to analyze text with machine learning, they put together an R package for MonkeyLearn’s API

To perform sentiment analysis in R using this package and MonkeyLearn, just follow these five simple steps:

1. Install the MonkeyLearn R package

First you’ll need to install the MonkeyLearn R Package using the following command: 


2. Load The Packages

Then, upload the package in your environment:


3. Set Your API Key

To use MonkeyLearn’s models through its API, you’ll need an API key. Just sign up to MonkeyLearn for free, then head to My account > API Key.

Now, save your API key as an environment variable:


All functions of the package will conveniently look for your API key using:


4. Set Up The Texts to Analyze by Sentiment 

In this step, you’ll need to define the texts you want to analyze with MonkeyLearn: 

text1 <- "this is really bad" 
text2 <- "i love this" 
texts <- c(text1, text2) 

5. Make A Request via The API

Finally, you’ll need to send a request through MonkeyLearn’s API to MonkeyLearn's pre-trained sentiment analysis model using the monkey_classify function:

monkey_classify(texts, classifier_id = "cl_pi3C7JiL") %>%

This will tell the pre-trained sentiment analysis model to analyze the texts defined in step 4. This will return:

# A tibble: 2 x 5
# req                confidence category_id probability label   
# <chr>                     <dbl>     <int>     <dbl>    <chr>   
# 1 this is really bad      0.997    60333049   0.997   Negative
# 2 i love this             0.98     60333048   0.98    Positive

And that’s it! You’ve just learned how to do sentiment analysis in R. You can now use sentiment to analyze data at scale, get insights and make data-driven decisions.

Create Your Own Sentiment Analysis Model to Use in R

Pre-trained models for sentiment analysis are great because you can start analyzing data right away. But, if you want maximum accuracy, you should train a custom model that uses your own data and criteria for sentiment analysis. This will enable the machine learning model to learn from industry-specific expressions but also understand your criteria and what you consider positive or negative. 

You can use MonkeyLearn to create a custom model for sentiment analysis in a few simple steps:

1. Choose A Model

Go to the dashboard and click on ‘create model’. This action will prompt you to choose a model type. Choose “Classifier”:

MonkeyLEarn's model builder showing two types of model: classfier and extractor

2. Select Sentiment Analysis

Next, you’ll see different options to train a classifier. Choose “Sentiment Analysis”:

MonkeyLearn's model builder showing three types of classification: topic analysis, sentiment analysis, and intent classification

3. Upload Your Data

You need to upload the data you want to use to train your sentiment analysis. Either upload it in an Excel or CSV file, or you use one of our many integrations to import your data: 

MonkeyLearn's model builder showing options to import data: Excel, CSV, data library

4. Train Your Sentiment Analysis Model

Here comes the most important step in creating your model: training. You’ll need to assign each text a sentiment tag (Positive, Neutral, or Negative). Once you’ve tagged several samples manually, you’ll notice the model will start making predictions on its own:

5. Test Your Sentiment Analysis Model

In this step, you need to make sure the model delivers accurate results. You can type text manually into the text box or upload a batch of reviews to see the confidence level of your model when making predictions:

You can continue to tag new data if the results are not accurate enough. The more data you feed your machine learning model, and the more data you tag, the better it will be at making predictions.

6. Call Your Sentiment Analysis Model API in R

You can call your new sentiment analysis model using the MonkeyLearn R package in the previous tutorial::

monkey_classify(texts, classifier_id = "ADD THE MODEL ID HERE", unnest = FALSE))

And there you have it. A quick and easy way to analyze text with R using a custom machine learning model!

Wrap up

Sentiment analysis has become a game-changer for companies that want to gain valuable insights from analyzing customer data.

The MonkeyLearn R package makes sentiment analysis in R simple and straightforward. Instead of creating machine learning models yourself, you can use MonkeyLearn’s pre-trained sentiment analysis model and start analyzing data right away. Alternatively, you can build your own custom model for even more accurate results.

Want to learn more about how MonkeyLearn can help you. Schedule a demo and we’ll be happy to show you.

Federico Pascual

December 27th, 2019

Posts you might like...

MonkeyLearn Logo

Text Analysis with Machine Learning

Turn tweets, emails, documents, webpages and more into actionable data. Automate business processes and save hours of manual data processing.

Try MonkeyLearn
Clearbit LogoSegment LogoPubnub LogoProtagonist Logo