-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCH6_case_study.qmd
385 lines (233 loc) · 7.09 KB
/
CH6_case_study.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
---
title: "Chapter 6 - Case Study"
author: "Government Analysis Function and ONS Data Science Campus"
engine: knitr
execute:
echo: true
eval: false
freeze: auto # re-render only when source changes
---
> To switch between light and dark modes, use the toggle in the top left
# 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.**
## 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.
## Example
::: {.panel-tabset}
### **Question**{-}
This is an example question.
### **Solution**{-}
```{r}
# Solution cell
"Insert code here"
```
:::
# Question 1: Packages
::: {.panel-tabset}
## **Question**{-}
Load the following packages:
* tidyverse
* janitor
## **Show Answer**{-}
```{r }
# load packages
library(tidyverse)
library(janitor)
```
:::
# Question 2: Data
::: {.panel-tabset}
## **Question**{-}
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](https://github.com/rfordatascience/tidytuesday) 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.
## **Show Answer**{-}
```{r}
# Read in imdb and netflix data
netflix <- readr::read_csv("Data/netflix_data.csv")
imdb_scores <- readr::read_csv("Data/imdb_scores.csv")
```
:::
# Question 3
::: {.panel-tabset}
## **Question**{-}
Clean up the column names of imdb_scores
## **Show Answer**{-}
```{r}
# Use janitor to clean names of imdb data
imdb_scores <- clean_names(imdb_scores)
names(imdb_scores)
```
:::
# Question 4
::: {.panel-tabset}
## **Question**{-}
What are the dimensions of the Netflix data?
See if you can output them in a sentence.
## **Show Answer**{-}
```{r}
# Find the dimensions with dim()
dim(netflix) # Rows and columns
```
```{r}
# Output a sentence with the dimensions
cat("There are", nrow(netflix), "rows and", ncol(netflix), "columns in the neflix dataset.")
```
:::
# Question 5
::: {.panel-tabset}
## **Question**{-}
Use an inspection function to determine the datatypes of the columns in the Netflix data.
## **Show Answer**{-}
```{r}
# Have a glimpse of netflix
glimpse(netflix)
```
:::
# Question 6
::: {.panel-tabset}
## **Question**{-}
How many missing values do we have in each dataset?
## **Show Answer**{-}
```{r}
# Number of missings in the netflix data
colSums(is.na(netflix))
```
```{r}
# Number of missings in imdb data
colSums(is.na(imdb_scores))
```
:::
# Question 7
::: {.panel-tabset}
## **Question**{-}
How many times does each unique country occur in the dataset?
## **Show Answer** {-}
```{r}
# Number of unique categories in primary_country
netflix |>
count(primary_country)
```
:::
# Question 8
::: {.panel-tabset}
## **Question**{-}
Create a new tibble "netflix_movies" by filtering the netflix tibble to contain only "Movie".
## **Show Answer**{-}
```{r}
# Create a tibble with "Movie"s only
netflix_movies <- netflix |>
filter(type == "Movie")
glimpse(netflix_movies)
```
:::
# Question 9
::: {.panel-tabset}
## **Question**{-}
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!
## **Show Answer**{-}
```{r}
# 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)
```
:::
# Question 10
::: {.panel-tabset}
## **Question**{-}
Using your netflix_movies tibble, compute:
* The mean and median duration of the movies
* The mean and standard deviation of the cast numbers.
## **Show Answer**{-}
```{r}
# 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))
```
:::
# Question 11
::: {.panel-tabset}
## **Question**{-}
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?
## **Show Answer**{-}
```{r}
# 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.
:::
# Question 12
::: {.panel-tabset}
## **Question**{-}
Using your netflix_movies tibble:
Group by primary_country and obtain the median cast number.
## **Show Answer**{-}
```{r}
# Group by country
netflix_movies |>
group_by(primary_country) |>
summarise(var_cast = median(num_cast, na.rm = TRUE))
```
:::
# Question 13
::: {.panel-tabset}
## **Question**{-}
Using your netflix_movies tibble:
Group by type and rating of the movie, producing the mean duration.
## **Show Answer**{-}
```{r}
# Group by type and rating
netflix_movies |>
group_by(type, rating) |>
summarise(mean_duration = mean(duration_mins, na.rm = TRUE))
```
:::
# Question 14
::: {.panel-tabset}
## **Question**{-}
Left join the imdb_scores data to the **original** netflix data.
Create a new variable netflix_imdb to contain this.
## **Show Answer**{-}
```{r}
# Join imdb and netflix
netflix_imdb <- netflix |>
left_join(y = imdb_scores,
by = "title")
glimpse(netflix_imdb)
```
:::
# 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:
* [Kaggle](https://www.kaggle.com/)
* [Tidy Tuesday](https://github.com/rfordatascience/tidytuesday)
* [Data.gov](https://data.gov.uk/)