Chapter 6 - Case Study

Author

Government Analysis Function and ONS Data Science Campus

To switch between light and dark modes, use the toggle in the top left

1 Introduction

By the end of this case study, you should have more confidence with manipulating data and using techniques from the first five chapters of Intro to R, as such, they are a pre-requisite for it.

These data sets and question are designed to be an initial springboard for you to continue with your data journey.

Answers are provided; but these may only show one or two ways of solving the issue.

Your answers may differ slightly from ours, this is fine if the output is consistent, but consider whether you could achieve your answer with less or better written code.

1.1 Structure:

Questions will be presented in tabs.

  • Tab 1 will contain the question
  • Tab 2 will contain the solution in R.

Please choose the tab with the language you wish to use. An example is below.

1.2 Example

This is an example question.

# Solution cell

"Insert code here"

2 Question 1: Packages

Load the following packages:

  • tidyverse
  • janitor
# load packages

library(tidyverse)
library(janitor)

3 Question 2: Data

Read in the two files from the data folder below, assigning them to the variables suggested:

netflix - nextflix_data.csv imdb_scores - imdb_scores.csv

Note - The data is sourced from Tidy Tuesday and directly from IMDB.

Some data has been altered to suit the difficulty level of this course. This is a training dataset, and so shouldn’t be relied upon for 100% accuracy.

# Read in imdb and netflix data

netflix <- readr::read_csv("Data/netflix_data.csv")

imdb_scores <- readr::read_csv("Data/imdb_scores.csv")

4 Question 3

Clean up the column names of imdb_scores

# Use janitor to clean names of imdb data

imdb_scores <- clean_names(imdb_scores)

names(imdb_scores)

5 Question 4

What are the dimensions of the Netflix data?

See if you can output them in a sentence.

# Find the dimensions with dim()

dim(netflix) # Rows and columns
# Output a sentence with the dimensions

cat("There are", nrow(netflix), "rows and", ncol(netflix), "columns in the neflix dataset.")

6 Question 5

Use an inspection function to determine the datatypes of the columns in the Netflix data.

# Have a glimpse of netflix

glimpse(netflix)

7 Question 6

How many missing values do we have in each dataset?

# Number of missings in the netflix data


colSums(is.na(netflix))
# Number of missings in imdb data

colSums(is.na(imdb_scores))

8 Question 7

How many times does each unique country occur in the dataset?

# Number of unique categories in primary_country

netflix |> 
    count(primary_country)

9 Question 8

Create a new tibble “netflix_movies” by filtering the netflix tibble to contain only “Movie”.

# Create a tibble with "Movie"s only

netflix_movies <- netflix |> 
    filter(type == "Movie")

glimpse(netflix_movies)

10 Question 9

Using your netflix_movies tibble, clean the duration column by:

  • Removing the suffix “min”.
  • Converting the resulting column to an integer

Following this, rename the column to “duration_mins”.

Note, you can do this in one pipeline!

Ensuring that you overwrite and reassign the dataset!

# Use mutate to clean the duration column

netflix_movies <- netflix_movies |> 
    mutate(duration = as.integer(str_replace(duration, 
                                             pattern = "min",
                                             replacement = ""))) |> 
    rename(duration_mins = duration)

glimpse(netflix_movies)

11 Question 10

Using your netflix_movies tibble, compute:

  • The mean and median duration of the movies
  • The mean and standard deviation of the cast numbers.
# Compute summary statistics of duration and cast number

netflix_movies |> 
    summarise(mean_duration = mean(duration_mins, na.rm = TRUE),
              median_duration = median(duration_mins, na.rm = TRUE),
              mean_cast = mean(num_cast, na.rm = TRUE),
              std_cast = sd(num_cast, na.rm = TRUE))

12 Question 11

Using your netflix_movies tibble:

  • Select the title, duration, director and cast numbers
  • Sort in descending order of duration

Which movie was the longest, and who directed it?

# Pipeline for longest movie

netflix_movies |> 
    select(title, duration_mins, director, num_cast) |> 
    arrange(desc(duration_mins)) |> 
    glimpse()

The longest movie on Netflix is Black Mirror: Bandersnatch, at 312 minutes, with no recorded director.

13 Question 12

Using your netflix_movies tibble:

Group by primary_country and obtain the median cast number.

# Group by country

netflix_movies |> 
    group_by(primary_country) |> 
    summarise(var_cast = median(num_cast, na.rm = TRUE))

14 Question 13

Using your netflix_movies tibble:

Group by type and rating of the movie, producing the mean duration.

# Group by type and rating

netflix_movies |> 
    group_by(type, rating) |> 
    summarise(mean_duration = mean(duration_mins, na.rm = TRUE))

15 Question 14

Left join the imdb_scores data to the original netflix data.

Create a new variable netflix_imdb to contain this.

# Join imdb and netflix

netflix_imdb <- netflix |> 
    left_join(y = imdb_scores,
              by = "title")

glimpse(netflix_imdb)

16 Summary

In this case study you have had the opportunity to apply data analysis techniques with the tidyverse to some additional datasets.

This is not exhaustive; have a look at the data and experiment with other techniques you can use.

This data has been provided for you to experiment with; however there is nothing better than learning with data that is meaningful to you.

For additional datasets we recommend exploring: