
Sentiment analysis helps us understand emotions in texts. In R, we can easily perform sentiment analysis using various packages. First, we load our text data and clean it to remove unnecessary words or symbols. Then, we apply the sentiment analysis methods from these packages.
Installing Necessary Packages
First, you’ll need to install a few R packages to do sentiment analysis in R.
install.packages("tm")
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidytext")
install.packages("textdata")
library(tm)
library(ggplot2)
library(dplyr)
library(tidytext)
library(textdata)
Loading the Text Data
You can download the dataset that we will be using from Kaggle. Then, load it into R.
# Load the CSV file
text_data <- read.csv("news_sentiment_analysis.csv")
# Extract the column that contains text
text_column <- text_data$Title
# View the first few entries of the extracted column
head(text_column)

Preprocessing the Text Data
It is important to clean and preprocess the text data. We convert the text to lowercase to ensure consistency. Next, we remove punctuation, so it doesn’t affect the sentiment analysis.
# Convert to lowercase
text_column <- tolower(text_column)
# Remove punctuation
text_column <- gsub("[[:punct:]]", "", text_column)
# Print the first few rows of the preprocessed text data
head(text_column)

Performing Sentiment Analysis
R provides various sentiment lexicons that can be applied to text data for analyzing sentiments. Below, we use three popular lexicons: Bing, NRC, and Afinn.
Using the Bing Lexicon
The Bing lexicon categorizes words as positive or negative. This makes it simple to determine the overall feeling of the text.
# Convert to data frame
text_df <- data.frame(text = text_column, stringsAsFactors = FALSE)
# Convert to lowercase
text_df$text <- tolower(text_df$text)
# Remove punctuation
text_df$text <- gsub("[[:punct:]]", "", text_df$text)
# Unnest the text into words
text_words <- text_df %>%
unnest_tokens(word, text)
# Load the Bing lexicon
bing <- get_sentiments("bing")
# Join the text words with the Bing lexicon
sentiment_analysis_bing <- text_words %>%
inner_join(bing, by = "word")
# Summarize sentiment counts
sentiment_summary_bing <- sentiment_analysis_bing %>%
count(sentiment, sort = TRUE)
# Print the sentiment summary for Bing
print(sentiment_summary_bing)
# Create a bar chart of sentiment counts for Bing
ggplot(sentiment_summary_bing, aes(x = sentiment, y = n, fill = sentiment)) +
geom_bar(stat = "identity") +
labs(title = "Sentiment Analysis Using Bing Lexicon",
x = "Sentiment",
y = "Count") +
theme_minimal()


The bar chart displays the sentiment analysis results using the Bing lexicon. It shows two main categories: positive and negative sentiments.
Using the NRC Lexicon
The NRC lexicon provides sentiment scores for various emotions, such as anger, fear, joy, and sadness. It sorts words into positive and negative feelings. This helps us understand the emotions in the text.
# Unnest the text into words
text_words <- text_df %>%
unnest_tokens(word, text)
# Load the NRC lexicon
nrc <- get_sentiments("nrc")
# Join the text words with the NRC lexicon
sentiment_analysis <- text_words %>%
inner_join(nrc, by = "word")
# Summarize sentiment counts
sentiment_summary <- sentiment_analysis %>%
count(sentiment, sort = TRUE)
# Print the sentiment summary
print(sentiment_summary)
# Create a bar chart of sentiment counts
ggplot(sentiment_summary, aes(x = reorder(sentiment, n), y = n, fill = sentiment)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(title = "Sentiment Analysis Using NRC Lexicon",
x = "Sentiment",
y = "Count") +
theme_minimal()


The chart displays the results of sentiment analysis using the NRC lexicon. It shows the different sentiment categories, such as joy, anger, sadness, and more.
Using the Afinn Lexicon
The Afinn lexicon assigns numerical values to words, indicating the strength of positive or negative sentiment on a scale from -5 (highly negative) to +5 (highly positive). This lexicon helps you look closely at how strong the feelings are in the text.
# Convert to data frame (same as above)
text_df <- data.frame(text = text_column, stringsAsFactors = FALSE)
# Convert to lowercase (same as above)
text_df$text <- tolower(text_df$text)
# Remove punctuation (same as above)
text_df$text <- gsub("[[:punct:]]", "", text_df$text)
# Unnest the text into words (same as above)
text_words <- text_df %>%
unnest_tokens(word, text)
# Load the AFINN lexicon
afinn <- get_sentiments("afinn")
# Join the text words with the AFINN lexicon
sentiment_analysis_afinn <- text_words %>%
inner_join(afinn, by = "word")
# Summarize sentiment scores
sentiment_summary_afinn <- sentiment_analysis_afinn %>%
group_by(value) %>%
summarise(count = n(), .groups = 'drop') %>%
arrange(desc(value))
# Print the sentiment summary for AFINN
print(sentiment_summary_afinn)
# Create a bar chart of sentiment scores for AFINN
ggplot(sentiment_summary_afinn, aes(x = reorder(as.factor(value), count), y = count, fill = as.factor(value))) +
geom_bar(stat = "identity") +
coord_flip() +
labs(title = "Sentiment Analysis Using AFINN Lexicon",
x = "Sentiment Score",
y = "Count") +
theme_minimal()


The plot illustrates the results of sentiment analysis using the AFINN lexicon. It focuses on the sentiment scores assigned to words.
Conclusion
Sentiment analysis is a useful tool to understand emotions in text. Using R, you can analyze text data with different lexicons like Bing, NRC, and Afinn. Each lexicon provides unique insights into sentiments and emotions.
