Introduction

In this lab, I used R to analyze five customer reviews and identify common words and customer opinions.

1. Load Packages

library(tidytext)
library(dplyr)
library(ggplot2)

2. Customer Reviews

reviews <- data.frame(
  id = 1:5,
  text = c(
    "I love the quality of this product. The price is reasonable, and delivery was fast.",
    "The product quality is excellent, but the shipping took too long.",
    "Great customer service! I would definitely recommend this product to my friends.",
    "The price is too expensive. However, the product looks beautiful.",
    "Fast delivery and excellent service. I am very satisfied with my purchase."
  )
)

reviews
##   id
## 1  1
## 2  2
## 3  3
## 4  4
## 5  5
##                                                                                  text
## 1 I love the quality of this product. The price is reasonable, and delivery was fast.
## 2                   The product quality is excellent, but the shipping took too long.
## 3    Great customer service! I would definitely recommend this product to my friends.
## 4                   The price is too expensive. However, the product looks beautiful.
## 5          Fast delivery and excellent service. I am very satisfied with my purchase.

3. Tokenization

words <- reviews %>%
  unnest_tokens(word, text)

head(words)
##   id    word
## 1  1       i
## 2  1    love
## 3  1     the
## 4  1 quality
## 5  1      of
## 6  1    this

4. Remove Stop Words

clean_words <- words %>%
  anti_join(stop_words, by = "word")

head(clean_words)
##   id       word
## 1  1       love
## 2  1    quality
## 3  1    product
## 4  1      price
## 5  1 reasonable
## 6  1   delivery

5. Word Frequency

word_count <- clean_words %>%
  count(word, sort = TRUE)

word_count
##          word n
## 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

6. Word Frequency Graph

word_count %>%
  slice_max(n, n = 10, with_ties = FALSE) %>%
  ggplot(aes(x = reorder(word, n), y = n)) +
  geom_col(fill = "steelblue") +
  coord_flip() +
  labs(
    title = "Most Common Words",
    x = "Words",
    y = "Frequency"
  ) +
  theme_minimal()

7. Sentiment Analysis

sentiment <- words %>%
  inner_join(get_sentiments("bing"), by = "word") %>%
  count(sentiment)

sentiment
##   sentiment  n
## 1  negative  1
## 2  positive 10

8. TF-IDF

tfidf <- clean_words %>%
  count(id, word) %>%
  bind_tf_idf(word, id, n)

head(arrange(tfidf, desc(tf_idf)), 10)
##    id       word n        tf      idf    tf_idf
## 1   2   shipping 1 0.2500000 1.609438 0.4023595
## 2   4  beautiful 1 0.2500000 1.609438 0.4023595
## 3   4  expensive 1 0.2500000 1.609438 0.4023595
## 4   3   customer 1 0.2000000 1.609438 0.3218876
## 5   3    friends 1 0.2000000 1.609438 0.3218876
## 6   3  recommend 1 0.2000000 1.609438 0.3218876
## 7   5   purchase 1 0.1666667 1.609438 0.2682397
## 8   5  satisfied 1 0.1666667 1.609438 0.2682397
## 9   1       love 1 0.1428571 1.609438 0.2299197
## 10  1 reasonable 1 0.1428571 1.609438 0.2299197

Findings

The word “product” appeared most often in the reviews. Most feedback was positive, but some customers mentioned high prices and slow shipping.

Challenge

One challenge was removing unnecessary words. I used anti_join(stop_words) to clean the reviews and focus on important words.