---
title: "Live coding session 2"
author: "Bodo"
date: "05/07/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Setup

In this script, we will plot the ratings for reviews containing "good" and "bad".

First, load packages:

```{r, warning = FALSE, message = FALSE}
library(tidyverse)
library(tidytext)
```

Second, load data:

```{r, warning = FALSE, message = FALSE}
yelp <- read_csv('../data/yelp_10000.csv')

# Check:

yelp
```

Check the language column:

```{r}
yelp %>% count(Language)
```

Get rid of unwanted columns:

```{r}
yelp <- select(yelp, review_id, stars, text)
```

Unnest tokens:

```{r}
yelp <- unnest_tokens(yelp, word, text)

# Check:

yelp
```

Reduce to "good" and "bad":

```{r}
good_bad <- filter(yelp,
                   word %in% c('good', 'bad'))

# Check:

good_bad
```

## Descriptive statistics

Compute averages:

```{r}
good_bad %>% group_by(word) %>% 
  summarize(M = mean(stars),
            SD = sd(stars))
```

Make a plot of this. First, let's create counts:

```{r}
counts <- good_bad %>% 
  count(word, stars)

# Check:

counts
```

Columns:

```{r}
counts %>% 
  ggplot(aes(x = stars, y = n, fill = word)) +
  geom_col(position = 'dodge') +
  scale_y_continuous(limits = c(0, 2500), expand = c(0, 0)) +
  scale_fill_manual(values = c('tomato3', 'skyblue')) +
  theme_classic()
```












