R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

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.")

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."

Get the tidt text dataset

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
text_df <- tibble(line = 1:5, text = text)

text_df
## # A tibble: 5 × 2
##    line text                                                                    
##   <int> <chr>                                                                   
## 1     1 I love the quality of this product. The price is reasonable, and delive…
## 2     2 The product quality is excellent, but the shipping took too long.       
## 3     3 Great customer service! I would definitely recommend this product to my…
## 4     4 The price is too expensive. However, the product looks beautiful.       
## 5     5 Fast delivery and excellent service. I am very satisfied with my purcha…

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(tidytext)

text_df <- text_df  %>%
  unnest_tokens(word, text)
text_df
## # A tibble: 60 × 2
##     line 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
data(stop_words)

text_df <- text_df %>%
  anti_join(stop_words)
## Joining with `by = join_by(word)`
text_df
## # A tibble: 26 × 2
##     line 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
text_df %>%
  count(word, sort = TRUE)
## # 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
library(ggplot2)

text_df %>%
  count(word, sort = TRUE) %>%
  filter(n > 1) %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(n, word)) +
  geom_col() +
  labs(y = NULL)