15-sentiment

Author
Affiliation

Prof Amanda Luby

Carleton College
Stat 220 - Winter 2026

library(tidyverse)
library(tidytext) # functions for doing text analysis

Load the Data

Random (?) sample of 26,882 reviews of coursera courses (Source: Kaggle)

en_coursera_reviews <- read_csv("https://stat220-w26.github.io/data/en_coursera_sample.csv")
en_coursera_reviews
# A tibble: 26,882 × 5
   CourseId                    Review                      Label cld2  review_id
   <chr>                       <chr>                       <dbl> <chr>     <dbl>
 1 nurture-market-strategies   It would be better if the …     1 en            1
 2 nand2tetris2                Superb course. Great prese…     5 en            2
 3 schedule-projects           Excellent course!               5 en            3
 4 teaching-english-capstone-2 I'd recommend this course …     5 en            4
 5 machine-learning            This course was so effecti…     5 en            5
 6 python-network-data         Words cannot describe how …     5 en            6
 7 clinical-trials             Great course!                   5 en            7
 8 python-genomics             I didn't know anything abo…     3 en            8
 9 strategic-management        Loved everything about thi…     5 en            9
10 script-writing              No significant instruction…     1 en           10
# ℹ 26,872 more rows

Load Sentiment data

“bing”

bing_sentiments = get_sentiments("bing") %>%
  slice_sample(n = 20)

“afinn”

# A tibble: 20 × 2
   word         value
   <chr>        <dbl>
 1 powerful         2
 2 regretted       -2
 3 alone           -2
 4 destroying      -3
 5 arrogant        -2
 6 conciliate       2
 7 interest         1
 8 jesus            1
 9 care             2
10 anguished       -3
11 guilt           -3
12 protest         -2
13 bad             -3
14 frisky           2
15 stingy          -2
16 enchanted        2
17 yeees            2
18 fatiguing       -2
19 discontented    -2
20 lifesaver        4

Sentiment of each review

bing_review_scores <- en_coursera_reviews %>%
  unnest_tokens(word, Review) %>% 
  inner_join(bing_sentiments, by = "word") %>%
  group_by(review_id) %>%
  summarize(
    sum = (sum(sentiment == "positive") - sum(sentiment == "negative"))
  )

bing_review_scores
# A tibble: 28 × 2
   review_id   sum
       <dbl> <int>
 1       357     1
 2       773     1
 3      1452    -1
 4      2564    -1
 5      3361    -1
 6      4256     1
 7      4269    -1
 8      6672     1
 9      7552    -1
10      7963    -1
# ℹ 18 more rows