-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_time.Rmd
68 lines (57 loc) · 3.27 KB
/
play_time.Rmd
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
---
title: "Play Time"
---
So I want to see with wich movies I have wasted the most amount of time.
```{r message=FALSE, echo=FALSE, error=FALSE, warning=FALSE}
# load libraries
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
```
To analyse all the movies throughout all the libraries, we first load the existing `.csv` files with the data from Plex. We then combine them into one giant data frame.
```{r warning=FALSE, message=FALSE, error=FALSE}
all_movie_libraries <- list.files(path = "data", pattern = "*.csv", full.names = TRUE) %>%
lapply(read.csv) %>%
lapply(\(x) mutate(x, across(Audience.Rating, as.double))) %>%
bind_rows()
```
We then change the `Duration` from the format HH:MM:SS to an amount in minutes. Also we discard a lot of columns we don't need anymore.
```{r warning=FALSE, message=FALSE, error=FALSE}
all_movie_libraries <- all_movie_libraries %>%
separate(Duration, c('play_time_hours', 'play_time_minutes', 'play_time_seconds'), ':') %>%
mutate(play_time_hours = as.numeric(play_time_hours), play_time_minutes = as.numeric(play_time_minutes), play_time_seconds = as.numeric(play_time_seconds)) %>%
mutate(play_time_single_in_minutes = ceiling(play_time_hours*60 + play_time_minutes + play_time_seconds/60)) %>%
select(title = Title,view_count = View.Count, play_time_single_in_minutes)
head(all_movie_libraries)
```
```{r warning=FALSE, message=FALSE, error=FALSE, echo=FALSE}
row_count_before_filtering = nrow(all_movie_libraries)
```
Now let's filter out all movies that haven't been watched before.
```{r warning=FALSE, message=FALSE, error=FALSE}
all_movie_libraries = all_movie_libraries %>%
filter(view_count != 'N/A')
```
With this we reduced the rows in our data frame from `r row_count_before_filtering` to `r nrow(all_movie_libraries)`.
After that we multiply the amount of minutes with the amount of times the movie has been played. This gives us the total amount of time played.
```{r warning=FALSE, message=FALSE, error=FALSE}
all_movie_libraries <- all_movie_libraries %>%
mutate(view_count = as.numeric(view_count)) %>%
mutate(play_time_total_in_minutes = play_time_single_in_minutes * view_count) %>%
select(title, play_time_total_in_minutes)
head(all_movie_libraries)
```
And now sort the data frame in a descending order, pick the top 10 rows and make a bar plot.
```{r warning=FALSE, message=FALSE, error=FALSE}
all_movie_libraries <- all_movie_libraries %>%
arrange(desc(play_time_total_in_minutes)) %>%
head(10)
all_movie_libraries %>%
ggplot(aes(x=title, y=play_time_total_in_minutes, color=play_time_total_in_minutes)) +
geom_bar(stat="identity", fill="white") +
scale_x_discrete(guide = guide_axis(angle = 90)) +
geom_text(aes(label=play_time_total_in_minutes), position = position_dodge(1.9), size=3.5, vjust=1.6) +
labs(title = 'My most watched movies by time spent watching', y = 'Time watched in minutes', x = 'Movie Titles') +
theme_classic()
```