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.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

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

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
library(readr)

# Load the movies dataset
movies <- read_csv("https://gist.githubusercontent.com/tiangechen/b68782efa49a16edaf07dc2cdaa855ea/raw/0c794a9717f18b094eabab2cd6a6b9a226903577/movies.csv")
## Rows: 77 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Film, Genre, Lead Studio, Worldwide Gross
## dbl (4): Audience score %, Profitability, Rotten Tomatoes %, Year
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
one <- movies %>% 
  rename(
    movie_title = Film,
    release_year = Year
  )

head(one)
## # A tibble: 6 × 8
##   movie_title               Genre `Lead Studio` `Audience score %` Profitability
##   <chr>                     <chr> <chr>                      <dbl>         <dbl>
## 1 Zack and Miri Make a Por… Roma… The Weinstei…                 70          1.75
## 2 Youth in Revolt           Come… The Weinstei…                 52          1.09
## 3 You Will Meet a Tall Dar… Come… Independent                   35          1.21
## 4 When in Rome              Come… Disney                        44          0   
## 5 What Happens in Vegas     Come… Fox                           72          6.27
## 6 Water For Elephants       Drama 20th Century…                 72          3.08
## # ℹ 3 more variables: `Rotten Tomatoes %` <dbl>, `Worldwide Gross` <chr>,
## #   release_year <dbl>
two <- one %>% 
  select(
    movie_title,
    release_year,
    Genre,
    Profitability,
    `Rotten Tomatoes %`,
    `Audience score %`
  )

head(two)
## # A tibble: 6 × 6
##   movie_title               release_year Genre Profitability `Rotten Tomatoes %`
##   <chr>                            <dbl> <chr>         <dbl>               <dbl>
## 1 Zack and Miri Make a Por…         2008 Roma…          1.75                  64
## 2 Youth in Revolt                   2010 Come…          1.09                  68
## 3 You Will Meet a Tall Dar…         2010 Come…          1.21                  43
## 4 When in Rome                      2010 Come…          0                     15
## 5 What Happens in Vegas             2008 Come…          6.27                  28
## 6 Water For Elephants               2011 Drama          3.08                  60
## # ℹ 1 more variable: `Audience score %` <dbl>
three <- two %>% 
  filter(
    release_year > 2008,
    `Rotten Tomatoes %` > 80
  )
head(three)
## # A tibble: 6 × 6
##   movie_title          release_year Genre     Profitability `Rotten Tomatoes %`
##   <chr>                       <dbl> <chr>             <dbl>               <dbl>
## 1 Tangled                      2010 Animation         1.37                   89
## 2 My Week with Marilyn         2011 Drama             0.826                  83
## 3 Midnight in Paris            2011 Romence           8.74                   93
## 4 Jane Eyre                    2011 Romance           0                      85
## 5 Beginners                    2011 Comedy            4.47                   84
## 6 A Serious Man                2009 Drama             4.38                   89
## # ℹ 1 more variable: `Audience score %` <dbl>
four <- three %>% 
  mutate(
    rating_gap = `Rotten Tomatoes %` - `Audience score %`
  )

head(four)
## # A tibble: 6 × 7
##   movie_title          release_year Genre     Profitability `Rotten Tomatoes %`
##   <chr>                       <dbl> <chr>             <dbl>               <dbl>
## 1 Tangled                      2010 Animation         1.37                   89
## 2 My Week with Marilyn         2011 Drama             0.826                  83
## 3 Midnight in Paris            2011 Romence           8.74                   93
## 4 Jane Eyre                    2011 Romance           0                      85
## 5 Beginners                    2011 Comedy            4.47                   84
## 6 A Serious Man                2009 Drama             4.38                   89
## # ℹ 2 more variables: `Audience score %` <dbl>, rating_gap <dbl>

Question 5: arrange()

five <- four %>% 
  arrange(desc(`Rotten Tomatoes %`), desc(Profitability))

head(five)
## # A tibble: 6 × 7
##   movie_title          release_year Genre     Profitability `Rotten Tomatoes %`
##   <chr>                       <dbl> <chr>             <dbl>               <dbl>
## 1 Midnight in Paris            2011 Romence            8.74                  93
## 2 A Serious Man                2009 Drama              4.38                  89
## 3 Tangled                      2010 Animation          1.37                  89
## 4 (500) Days of Summer         2009 comedy             8.10                  87
## 5 Jane Eyre                    2011 Romance            0                     85
## 6 Beginners                    2011 Comedy             4.47                  84
## # ℹ 2 more variables: `Audience score %` <dbl>, rating_gap <dbl>

Question 6: Combining Functions

final_movies <- movies %>% 
  rename(
    movie_title = Film,
    release_year = Year
  ) %>% 
  select(
    movie_title,
    release_year,
    Genre,
    Profitability,
    `Rotten Tomatoes %`,
    `Audience score %`
  ) %>% 
  filter(
    release_year > 2008,
    `Rotten Tomatoes %` > 80
  ) %>% 
  mutate(
    rating_gap = `Rotten Tomatoes %` - `Audience score %`
  ) %>% 
  arrange(desc(`Rotten Tomatoes %`), desc(Profitability))

head(final_movies)
## # A tibble: 6 × 7
##   movie_title          release_year Genre     Profitability `Rotten Tomatoes %`
##   <chr>                       <dbl> <chr>             <dbl>               <dbl>
## 1 Midnight in Paris            2011 Romence            8.74                  93
## 2 A Serious Man                2009 Drama              4.38                  89
## 3 Tangled                      2010 Animation          1.37                  89
## 4 (500) Days of Summer         2009 comedy             8.10                  87
## 5 Jane Eyre                    2011 Romance            0                     85
## 6 Beginners                    2011 Comedy             4.47                  84
## # ℹ 2 more variables: `Audience score %` <dbl>, rating_gap <dbl>

Question 7

The movie with the highest critic score is Midnight in Paris with a 93% Rotten Tomatoes score, but it does not have the highest audience score because Tangled scored an 88% audience rating. The positive rating gap of 9 for Midnight in Paris reveals that critics rated the film higher than general audiences did. In contrast, A Serious Man shows an even larger gap of 25 points, meaning critics enjoyed the movie much more than audience members.