Podstawowe operacje w R - część 3.

Eksploracja danych

Agnieszka Matyka

2022-11-16

Spis treści:

Eksploracja danych z bibliotekami dplyr, tidyr oraz stringr
- Podzbiory kolumn
- Filtrowanie wierszy
- Operatory logiczne, algebra Boola, prawa de Morgana
- Tworzenie nowych kolumn (1x Challenge)
- Wartości brakujące
- Manipulowanie tekstem (3x Challenge)
- Agregacja danych (1x Challenge)
- Tabele przestawne, dane w formacie long oraz wide
- Łączenie tabel

Przydatne materiały:
- dplyr cheatsheet
- tidyr cheatsheet
- stringr cheatsheet
- ggplot2 cheatsheet
- A. Kassambara - Guide to Create Beautiful Graphics in R.

if(!require('tidyverse')) install.packages('tidyverse')
## Ładowanie wymaganego pakietu: tidyverse
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2
## Warning: pakiet 'ggplot2' został zbudowany w wersji R 4.2.2
## Warning: pakiet 'purrr' został zbudowany w wersji R 4.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidyverse)

Dane pochodzą ze strony https://flixgem.com/ (wersja zbioru danych z dnia 12 marca 2021). Dane zawierają informacje na temat 9425 filmów i seriali dostępnych na Netlixie.

dane$Release.Date <- as.Date(dane$Release.Date, format = "%d/%m/%Y")
dane$Netflix.Release.Date <- as.Date(dane$Netflix.Release.Date, format = "%d/%m/%Y")

CHALLENGE 1: Jaki jest najstarszy film Woody’ego Allena dostępny na Netflixie?

dane %>%
  filter(Director == 'Woody Allen') %>%
  arrange(Release.Date) %>%
  select(Title, Release.Date) %>%
  group_by(Release.Date) 
## # A tibble: 18 × 2
## # Groups:   Release.Date [9]
##    Title                                                              Release.…¹
##    <chr>                                                              <date>    
##  1 Everything You Always Wanted to Know About Sex But Were Afraid to… 1972-06-08
##  2 Alice                                                              1991-10-01
##  3 Whatever Works                                                     2009-03-07
##  4 Midnight in Paris                                                  2011-10-06
##  5 To Rome with Love                                                  2012-06-07
##  6 Irrational Man                                                     2015-07-08
##  7 Café Society                                                       2016-05-08
##  8 A Rainy Day in New York                                            2020-10-11
##  9 Sleeper                                                            NA        
## 10 Wonder Wheel                                                       NA        
## 11 A Midsummer Nights Sex Comedy                                      NA        
## 12 Love and Death                                                     NA        
## 13 Manhattan Murder Mystery                                           NA        
## 14 Magic in the Moonlight                                             NA        
## 15 Blue Jasmine                                                       NA        
## 16 Scoop                                                              NA        
## 17 Vicky Cristina Barcelona                                           NA        
## 18 You Will Meet a Tall Dark Stranger                                 NA        
## # … with abbreviated variable name ¹​Release.Date
#odp Everything You Always Wanted to Know About Sex But Were Afraid to Ask

CHALLENGE 2: Jakie są trzy najwyżej oceniane komedie dostępne w języku polskim?

dane %>%
  filter(Genre %>% str_detect("Comedy")) %>%
  filter(Languages %>% str_detect('Polish')) %>%
  arrange(-IMDb.Score) %>%
  select(Title, IMDb.Score) %>%
  top_n(3)
## Selecting by IMDb.Score
##                    Title IMDb.Score
## 1                   Rake        8.5
## 2             Teddy Bear        8.2
## 3 Trois Couleurs - Blanc        7.6

CHALLENGE 3: Dla produkcji z lat 2019 oraz 2020 jaki jest średni czas między premierą a pojawieniem się na Netflixie?

dane %>%
  drop_na(Release.Date ,Netflix.Release.Date) %>%
  mutate(rok = lubridate::year(Release.Date), roznica = Netflix.Release.Date - Release.Date) %>%
  filter(rok %>% between(2019,2020)) %>%
  group_by(rok) %>%
  summarize(srednia = mean(roznica) %>% round(0))
## # A tibble: 2 × 2
##     rok srednia
##   <dbl> <drtn> 
## 1  2019 97 days
## 2  2020  8 days

CHALLENGE 4: Jakie są najpopularniejsze tagi dla produkcji dostępnych w języku polskim?

dane %>%
  filter(Languages %>% str_detect('Polish')) %>%
  pull(Tags) %>%
  str_c(collapse = ",") %>%
  str_split(",") %>%
  table() %>%
  as.data.frame() %>%
  arrange(- Freq) %>%
  top_n(5)
## Selecting by Freq
##                       . Freq
## 1                Dramas   50
## 2         Polish Movies   29
## 3         Polish Dramas   17
## 4             TV Dramas   15
## 5  International Dramas   14
## 6 Movies Based on Books   14

CHALLENGE 5: Jakie są średnie oceny filmów wyprodukowanych w poszczególnych dekadach (tzn. lata 60, 70, 80, 90 etc.)?

dane$Release.Date <- as.Date(dane$Release.Date, format = "%m/%d/%Y")
dane %>%
  mutate(rok = lubridate::year(Release.Date), decade = floor(rok/10)*10) %>%
  group_by(decade) %>%
  summarize(count = n(),
            avg_imdb_score = mean(IMDb.Score, na.rm = TRUE) %>% round(2))
## # A tibble: 11 × 3
##    decade count avg_imdb_score
##     <dbl> <int>          <dbl>
##  1   1930     5           7.24
##  2   1940     4           7.88
##  3   1950     8           7.59
##  4   1960    30           7.48
##  5   1970    43           7.29
##  6   1980   121           7.24
##  7   1990   239           6.88
##  8   2000   678           6.91
##  9   2010  2483           7   
## 10   2020   241           7.05
## 11     NA  5573           6.93