two plus two equals 4.
## [1] 4
install.packages("tidyverse")
library("tidyverse")
## Rows: 240640 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): country_name, country_iso2, category, show_title, season_title
## dbl (2): weekly_rank, cumulative_weeks_in_top_10
## date (1): week
##
## ℹ 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.
## spc_tbl_ [240,640 × 8] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ country_name : chr [1:240640] "Argentina" "Argentina" "Argentina" "Argentina" ...
## $ country_iso2 : chr [1:240640] "AR" "AR" "AR" "AR" ...
## $ week : Date[1:240640], format: "2023-12-17" "2023-12-17" ...
## $ category : chr [1:240640] "Films" "Films" "Films" "Films" ...
## $ weekly_rank : num [1:240640] 1 2 3 4 5 6 7 8 9 10 ...
## $ show_title : chr [1:240640] "Leave the World Behind" "El Amor Menos Pensado" "Old" "The Lost City" ...
## $ season_title : chr [1:240640] "N/A" "N/A" "N/A" "N/A" ...
## $ cumulative_weeks_in_top_10: num [1:240640] 2 1 1 1 3 4 2 1 1 1 ...
## - attr(*, "spec")=
## .. cols(
## .. country_name = col_character(),
## .. country_iso2 = col_character(),
## .. week = col_date(format = ""),
## .. category = col_character(),
## .. weekly_rank = col_double(),
## .. show_title = col_character(),
## .. season_title = col_character(),
## .. cumulative_weeks_in_top_10 = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
## country_name country_iso2 week category
## Length:240640 Length:240640 Min. :2021-07-04 Length:240640
## Class :character Class :character 1st Qu.:2022-02-11 Class :character
## Mode :character Mode :character Median :2022-09-25 Mode :character
## Mean :2022-09-24
## 3rd Qu.:2023-05-07
## Max. :2023-12-17
## weekly_rank show_title season_title
## Min. : 1.0 Length:240640 Length:240640
## 1st Qu.: 3.0 Class :character Class :character
## Median : 5.5 Mode :character Mode :character
## Mean : 5.5
## 3rd Qu.: 8.0
## Max. :10.0
## cumulative_weeks_in_top_10
## Min. : 1.000
## 1st Qu.: 1.000
## Median : 2.000
## Mean : 3.574
## 3rd Qu.: 3.000
## Max. :103.000
can be helpful to have a summary in txt.file to have it quickly at
hand sink('summary.txt')
summary(german_netflix) sink()
each function is being performed independentally
select(german_netflix, country_name, country_iso2, week, weekly_rank, show_title, cumulative_weeks_in_top_10)## # A tibble: 240,640 × 6
## country_name country_iso2 week weekly_rank show_title
## <chr> <chr> <date> <dbl> <chr>
## 1 Argentina AR 2023-12-17 1 Leave the World Behind
## 2 Argentina AR 2023-12-17 2 El Amor Menos Pensado
## 3 Argentina AR 2023-12-17 3 Old
## 4 Argentina AR 2023-12-17 4 The Lost City
## 5 Argentina AR 2023-12-17 5 Women on the Edge
## 6 Argentina AR 2023-12-17 6 Leo
## 7 Argentina AR 2023-12-17 7 Nobody
## 8 Argentina AR 2023-12-17 8 Blood Vessel
## 9 Argentina AR 2023-12-17 9 Underwater
## 10 Argentina AR 2023-12-17 10 Chicken Run: Dawn of the Nu…
## # ℹ 240,630 more rows
## # ℹ 1 more variable: cumulative_weeks_in_top_10 <dbl>
## # A tibble: 258 × 8
## country_name country_iso2 week category weekly_rank show_title
## <chr> <chr> <date> <chr> <dbl> <chr>
## 1 Germany DE 2023-12-17 Films 1 Leave the World Be…
## 2 Germany DE 2023-12-17 TV 1 My Life With the W…
## 3 Germany DE 2023-12-10 Films 1 Leave the World Be…
## 4 Germany DE 2023-12-10 TV 1 My Life With the W…
## 5 Germany DE 2023-12-03 Films 1 Family Switch
## 6 Germany DE 2023-12-03 TV 1 A Nearly Normal Fa…
## 7 Germany DE 2023-11-26 Films 1 Leo
## 8 Germany DE 2023-11-26 TV 1 Squid Game: The Ch…
## 9 Germany DE 2023-11-19 Films 1 The Killer
## 10 Germany DE 2023-11-19 TV 1 The Crown
## # ℹ 248 more rows
## # ℹ 2 more variables: season_title <chr>, cumulative_weeks_in_top_10 <dbl>
if you want to perform a select & filter at same time, you’ll need a pipe macOS user: try ‘cmd+shift+m’ you should get %>%
german_netflix %>%
filter(country_iso2 == 'DE', cumulative_weeks_in_top_10 > 9) %>%
select(country_name, country_iso2, week, weekly_rank, show_title, cumulative_weeks_in_top_10)## # A tibble: 65 × 6
## country_name country_iso2 week weekly_rank show_title
## <chr> <chr> <date> <dbl> <chr>
## 1 Germany DE 2023-06-11 5 The Rookie
## 2 Germany DE 2023-06-11 6 The Rookie
## 3 Germany DE 2023-06-11 8 Manifest
## 4 Germany DE 2023-06-04 3 The Rookie
## 5 Germany DE 2023-05-28 9 The Night Agent
## 6 Germany DE 2023-04-02 9 All Quiet on the Western Fr…
## 7 Germany DE 2023-03-26 3 All Quiet on the Western Fr…
## 8 Germany DE 2023-03-19 2 All Quiet on the Western Fr…
## 9 Germany DE 2023-03-12 6 All Quiet on the Western Fr…
## 10 Germany DE 2023-03-05 9 All Quiet on the Western Fr…
## # ℹ 55 more rows
## # ℹ 1 more variable: cumulative_weeks_in_top_10 <dbl>
output top5, sort by date, minimum 4 weeks in top 10
german_netflix %>%
filter(country_iso2 == 'DE', cumulative_weeks_in_top_10 > 3) %>%
select(country_name, week, weekly_rank, show_title,
cumulative_weeks_in_top_10) %>%
distinct(show_title, .keep_all = TRUE) %>%
group_by(show_title) %>%
slice_max(order_by = cumulative_weeks_in_top_10) %>%
ungroup() %>%
arrange(desc(week)) %>%
print(n=5)## # A tibble: 156 × 5
## country_name week weekly_rank show_title cumulative_weeks_in_…¹
## <chr> <date> <dbl> <chr> <dbl>
## 1 Germany 2023-12-17 8 A Nearly Normal Fa… 4
## 2 Germany 2023-12-17 4 Dr. Seuss' The Gri… 7
## 3 Germany 2023-12-17 6 Leo 4
## 4 Germany 2023-12-17 3 The Crown 5
## 5 Germany 2023-12-03 9 All the Light We C… 5
## # ℹ 151 more rows
## # ℹ abbreviated name: ¹cumulative_weeks_in_top_10
In some previous output you’ll saw duplicates in show_title. We want to get rid of those duplicates and instead apply logic of distinct titles, which shows the longest period of weeks that’ll was in top 10, plus filter and select only with minimun of 4 weeks.
This will give you a data frame with distinct ‘show_title’ values and corresponding rows where ‘cumulative_weeks_in_top_10’ is the highest among duplicates.
install.packages("readr")
export will flow into your current working-directory
german_netflix %>%
filter(country_iso2 == 'DE', cumulative_weeks_in_top_10 > 4) %>%
select(country_name, country_iso2, week, weekly_rank, show_title, cumulative_weeks_in_top_10) %>%
distinct(show_title, .keep_all = TRUE) %>%
group_by(show_title) %>%
slice_max(order_by = cumulative_weeks_in_top_10) %>%
ungroup() %>%
arrange(desc(week)) %>%
write_csv("germanNetflixTitle4weeksOrMore.csv")