This analysis uses the tidytext approach to examine a
small sample of social media customer comments. The goal is to identify
commonly used words and better understand the major themes appearing in
customer feedback.
# Install packages only if they are not already installed
packages <- c("tidyverse", "tidytext")
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages) > 0) {
install.packages(new_packages)
}
# Load packages
library(tidyverse)
library(tidytext)
Following the tidy text format, each individual word is converted
into its own row using unnest_tokens().
tidy_posts <- social_media %>%
unnest_tokens(word, text)
tidy_posts
## # A tibble: 60 × 2
## post_id word
## <int> <chr>
## 1 1 i
## 2 1 love
## 3 1 the
## 4 1 quality
## 5 1 of
## 6 1 this
## 7 1 product
## 8 1 the
## 9 1 price
## 10 1 is
## # ℹ 50 more rows
This creates a one-token-per-row structure, making it easier to analyze individual words across all five comments.
Common words such as “the,” “is,” “and,” and “with” usually provide
little information about the main themes of a text. These stop words can
be removed using the stop_words dataset from the
tidytext package.
tidy_posts_clean <- tidy_posts %>%
anti_join(stop_words, by = "word")
tidy_posts_clean
## # A tibble: 26 × 2
## post_id word
## <int> <chr>
## 1 1 love
## 2 1 quality
## 3 1 product
## 4 1 price
## 5 1 reasonable
## 6 1 delivery
## 7 1 fast
## 8 2 product
## 9 2 quality
## 10 2 excellent
## # ℹ 16 more rows
Next, the remaining words are counted and sorted from most frequent to least frequent.
word_counts <- tidy_posts_clean %>%
count(word, sort = TRUE)
word_counts
## # A tibble: 17 × 2
## word n
## <chr> <int>
## 1 product 4
## 2 delivery 2
## 3 excellent 2
## 4 fast 2
## 5 price 2
## 6 quality 2
## 7 service 2
## 8 beautiful 1
## 9 customer 1
## 10 expensive 1
## 11 friends 1
## 12 love 1
## 13 purchase 1
## 14 reasonable 1
## 15 recommend 1
## 16 satisfied 1
## 17 shipping 1
The bar chart below displays the most frequently occurring meaningful words in the customer comments.
word_counts %>%
filter(n >= 1) %>%
slice_max(n, n = 15, with_ties = FALSE) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(x = word, y = n)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(
title = "Most Common Words in Customer Comments",
x = "Word",
y = "Frequency"
) +
theme_minimal()
The Bing sentiment lexicon classifies words as either positive or negative. This allows us to examine the overall sentiment expressed in the customer comments.
bing_sentiments <- get_sentiments("bing")
sentiment_words <- tidy_posts_clean %>%
inner_join(bing_sentiments, by = "word")
sentiment_words
## # A tibble: 10 × 3
## post_id word sentiment
## <int> <chr> <chr>
## 1 1 love positive
## 2 1 reasonable positive
## 3 1 fast positive
## 4 2 excellent positive
## 5 3 recommend positive
## 6 4 expensive negative
## 7 4 beautiful positive
## 8 5 fast positive
## 9 5 excellent positive
## 10 5 satisfied positive
sentiment_counts <- sentiment_words %>%
count(sentiment)
sentiment_counts
## # A tibble: 2 × 2
## sentiment n
## <chr> <int>
## 1 negative 1
## 2 positive 9
sentiment_counts %>%
ggplot(aes(x = sentiment, y = n, fill = sentiment)) +
geom_col(show.legend = FALSE) +
labs(
title = "Sentiment of Customer Comments",
x = "Sentiment",
y = "Number of Words"
) +
theme_minimal()
The following analysis compares the number of positive and negative words found in each individual customer comment.
post_sentiment <- sentiment_words %>%
count(post_id, sentiment)
post_sentiment
## # A tibble: 6 × 3
## post_id sentiment n
## <int> <chr> <int>
## 1 1 positive 3
## 2 2 positive 1
## 3 3 positive 1
## 4 4 negative 1
## 5 4 positive 1
## 6 5 positive 3
post_sentiment %>%
ggplot(aes(
x = factor(post_id),
y = n,
fill = sentiment
)) +
geom_col(position = "dodge") +
labs(
title = "Positive and Negative Sentiment by Post",
x = "Post ID",
y = "Number of Sentiment Words",
fill = "Sentiment"
) +
theme_minimal()
The text analysis shows that customer comments focus primarily on product quality, price, delivery, and service. Positive words such as love, excellent, great, beautiful, satisfied, and recommend indicate that customers generally express favorable opinions about the product and their experiences.
However, some negative feedback is also present. In particular, customers mention that the product can be expensive and that shipping can take too long. Overall, the sample suggests generally positive customer sentiment, while price and shipping speed represent potential areas for improvement.
Using the tidytext framework makes it possible to
transform unstructured social media comments into data that can be
analyzed quantitatively. Word-frequency and sentiment analysis help
identify common customer concerns and provide businesses with insights
into what customers value and what aspects of the customer experience
may need improvement.