REQUIRED PACKAGE
library(tidyverse) ## dplyr provides the join functions
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.3
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
After Download Data from Link Above
Load the data to your Working Directory with code below
load("/home/mustafa-omer/İndirilenler/atp_tennis_data_2017.RData") # Change Path according to your data location you may use file.chose() function to reach your data path
DATA EXAMINATION
str(player_df)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 10912 obs. of 13 variables:
## $ player_id : chr "a002" "a001" "a005" "a004" ...
## $ player_slug: chr "ricardo-acuna" "sadiq-abdullahi" "nelson-aerts" "egan-adams" ...
## $ first_name : chr "Ricardo" "Sadiq" "Nelson" "Egan" ...
## $ last_name : chr "Acuna" "Abdullahi" "Aerts" "Adams" ...
## $ flag_code : chr "CHI" "NGR" "BRA" "USA" ...
## $ residence : chr "Jupiter, FL, USA" NA NA "Palmetto, FL, USA" ...
## $ birth_place: chr "Santiago, Chile" NA "Cachoeira Do Sul, Brazil" "Miami Beach, FL, USA" ...
## $ birth_date : Date, format: "1958-01-13" "1960-02-02" ...
## $ turned_pro : num 0 0 0 0 1983 ...
## $ weight_kg : num 68 0 75 73 82 68 0 0 0 82 ...
## $ height_cm : num 175 0 188 178 180 183 0 0 0 191 ...
## $ handedness : chr NA NA NA NA ...
## $ backhand : chr NA NA NA NA ...
str(rank_df)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 87740 obs. of 6 variables:
## $ week_title : Date, format: "2017-11-20" "2017-11-20" ...
## $ player_id : chr "n409" "f324" "d875" "z355" ...
## $ rank_number : num 1 2 3 4 5 6 7 8 9 10 ...
## $ ranking_points : num 10645 9605 5150 4610 4015 ...
## $ tourneys_played: num 18 17 23 25 27 22 26 22 15 25 ...
## $ player_age : num 31 36 26 20 24 29 26 25 32 26 ...
str(score_df)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 3830 obs. of 14 variables:
## $ match_id : chr "2017-339-d875-n552" "2017-339-d875-r975" "2017-339-n552-w367" "2017-339-r975-n409" ...
## $ tourney_id : chr "339" "339" "339" "339" ...
## $ tourney_round_name : chr "Finals" "Semi-Finals" "Semi-Finals" "Quarter-Finals" ...
## $ winner_player_id : chr "d875" "d875" "n552" "r975" ...
## $ loser_player_id : chr "n552" "r975" "w367" "n409" ...
## $ winner_seed : chr "7" "7" "3" "1" ...
## $ loser_seed : chr "3" "1" "2" "5" ...
## $ match_score_tiebreaks: chr "62 26 63" "76(7) 62" "76(3) 63" "46 63 64" ...
## $ winner_sets_won : num 2 2 2 2 2 2 2 2 2 2 ...
## $ loser_sets_won : num 1 0 0 1 1 0 1 0 0 1 ...
## $ winner_games_won : num 14 13 13 16 18 12 16 12 13 16 ...
## $ loser_games_won : num 11 8 9 13 15 2 12 5 10 13 ...
## $ winner_tiebreaks_won : num 0 1 1 0 0 0 0 0 1 0 ...
## $ loser_tiebreaks_won : num 0 0 0 0 1 0 0 0 0 0 ...
str(stats_df)
## Classes 'tbl_df', 'tbl' and 'data.frame': 3811 obs. of 54 variables:
## $ match_id : chr "2017-339-r975-n409" "2017-339-d875-n552" "2017-339-d875-r975" "2017-339-n552-w367" ...
## $ match_time : 'hms' num 02:21:00 01:49:00 01:28:00 01:42:00 ...
## ..- attr(*, "units")= chr "secs"
## $ match_duration : num 141 109 88 102 125 156 62 69 89 90 ...
## $ winner_aces : num 23 7 4 1 3 11 3 12 11 7 ...
## $ winner_double_faults : num 3 2 1 1 3 3 0 1 1 2 ...
## $ winner_first_serves_in : num 62 52 36 56 52 67 19 40 44 41 ...
## $ winner_first_serves_total : num 97 77 58 77 94 119 34 53 65 64 ...
## $ winner_first_serve_points_won : num 50 41 27 37 42 47 18 30 36 33 ...
## $ winner_first_serve_points_total : num 62 52 36 56 52 67 19 40 44 41 ...
## $ winner_second_serve_points_won : num 16 12 18 14 23 28 10 7 15 15 ...
## $ winner_second_serve_points_total: num 35 25 22 21 42 52 15 13 21 23 ...
## $ winner_break_points_saved : num 6 5 0 4 13 11 0 2 4 0 ...
## $ winner_break_points_serve_total : num 7 7 0 5 14 13 0 3 4 1 ...
## $ winner_service_points_won : num 66 53 45 51 65 75 28 37 51 48 ...
## $ winner_service_points_total : num 97 77 58 77 94 119 34 53 65 64 ...
## $ winner_first_serve_return_won : num 22 13 4 10 8 13 13 12 1 7 ...
## $ winner_first_serve_return_total : num 61 49 28 37 37 65 28 29 35 46 ...
## $ winner_second_serve_return_won : num 9 11 17 14 21 21 14 9 14 17 ...
## $ winner_second_serve_return_total: num 23 20 33 24 45 32 19 15 23 25 ...
## $ winner_break_points_converted : num 2 3 2 2 3 4 5 4 1 2 ...
## $ winner_break_points_return_total: num 4 5 4 2 7 10 8 6 4 5 ...
## $ winner_service_games_played : num 15 13 10 11 14 16 7 9 11 11 ...
## $ winner_return_games_played : num 14 12 10 10 14 16 7 8 10 11 ...
## $ winner_return_points_won : num 31 24 21 24 29 34 27 21 15 24 ...
## $ winner_return_points_total : num 84 69 61 61 82 97 47 44 58 71 ...
## $ winner_total_points_won : num 97 77 66 75 94 109 55 58 66 72 ...
## $ winner_total_points_total : num 181 146 119 138 176 216 81 97 123 135 ...
## $ loser_aces : num 4 4 4 9 6 2 1 0 11 5 ...
## $ loser_double_faults : num 0 0 3 2 5 2 2 1 1 7 ...
## $ loser_first_serves_in : num 61 49 28 37 37 65 28 29 35 46 ...
## $ loser_first_serves_total : num 84 69 61 61 82 97 47 44 58 71 ...
## $ loser_first_serve_points_won : num 39 36 24 27 29 52 15 17 34 39 ...
## $ loser_first_serve_points_total : num 61 49 28 37 37 65 28 29 35 46 ...
## $ loser_second_serve_points_won : num 14 9 16 10 24 11 5 6 9 8 ...
## $ loser_second_serve_points_total : num 23 20 33 24 45 32 19 15 23 25 ...
## $ loser_break_points_saved : num 2 2 2 0 4 6 3 2 3 3 ...
## $ loser_break_points_serve_total : num 4 5 4 2 7 10 8 6 4 5 ...
## $ loser_service_points_won : num 53 45 40 37 53 63 20 23 43 47 ...
## $ loser_service_points_total : num 84 69 61 61 82 97 47 44 58 71 ...
## $ loser_first_serve_return_won : num 12 11 9 19 10 20 1 10 8 8 ...
## $ loser_first_serve_return_total : num 62 52 36 56 52 67 19 40 44 41 ...
## $ loser_second_serve_return_won : num 19 13 4 7 19 24 5 6 6 8 ...
## $ loser_second_serve_return_total : num 35 25 22 21 42 52 15 13 21 23 ...
## $ loser_break_points_converted : num 1 2 0 1 1 2 0 1 0 1 ...
## $ loser_break_points_return_total : num 7 7 0 5 14 13 0 3 4 1 ...
## $ loser_service_games_played : num 14 12 10 10 14 16 7 8 10 11 ...
## $ loser_return_games_played : num 15 13 10 11 14 16 7 9 11 11 ...
## $ loser_return_points_won : num 31 24 13 26 29 44 6 16 14 16 ...
## $ loser_return_points_total : num 97 77 58 77 94 119 34 53 65 64 ...
## $ loser_total_points_won : num 84 69 53 63 82 107 26 39 57 63 ...
## $ loser_total_points_total : num 181 146 119 138 176 216 81 97 123 135 ...
## $ tourney_id : chr "339" "339" "339" "339" ...
## $ winner_player_id : chr "r975" "d875" "d875" "n552" ...
## $ loser_player_id : chr "n409" "n552" "r975" "w367" ...
str(tourney_df)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 67 obs. of 12 variables:
## $ tourney_id : num 339 891 451 338 301 ...
## $ tourney_name : chr "Brisbane International presented by Suncorp" "Aircel Chennai Open" "Qatar ExxonMobil Open" "Sydney International" ...
## $ tourney_location : chr "Brisbane, Australia" "Chennai, India" "Doha, Qatar" "Sydney, Australia" ...
## $ tourney_date : Date, format: "2017-01-01" "2017-01-02" ...
## $ tourney_singles_draw : num 28 28 32 28 28 128 28 28 28 28 ...
## $ tourney_doubles_draw : num 28 28 32 28 28 128 28 28 28 28 ...
## $ tourney_conditions : chr "Outdoor" "Outdoor" "Outdoor" "Outdoor" ...
## $ tourney_surface : chr "Hard" "Hard" "Hard" "Hard" ...
## $ tourney_fin_commit : chr "$495,630" "$505,730" "$1,334,270" "$495,630" ...
## $ singles_winner_player_id : chr "d875" "bd06" "d643" "ma30" ...
## $ doubles_winner_1_player_id: chr "kd46" "b757" "ca12" "kc41" ...
## $ doubles_winner_2_player_id: chr "tc61" "n480" "me04" "mb88" ...
QUESTION-1: Rank countries (flag cıdes) by the number of singles champions
Solution-1:
champ_flags_df<-tourney_df%>%left_join(.,player_df,by=c("singles_winner_player_id"="player_id"))%>%count(flag_code,sort = TRUE)
champ_flags_df
## # A tibble: 21 x 2
## flag_code n
## <chr> <int>
## 1 ESP 11
## 2 USA 9
## 3 SUI 8
## 4 FRA 7
## 5 GER 7
## 6 BUL 4
## 7 ARG 2
## 8 BEL 2
## 9 BIH 2
## 10 CRO 2
## # … with 11 more rows
QUESTION-2: Rank countries which did not get any singles championships by the games won when they win the match
Solution-2:
nonchamp_players<-player_df%>%select(player_id,flag_code)%>%anti_join(.,champ_flags_df)
## Joining, by = "flag_code"
loser_countries_rank<-nonchamp_players%>%left_join(.,score_df,by=c("player_id"="winner_player_id"))%>%group_by(flag_code)%>%summarise(total_won=sum(winner_games_won,na.rm = TRUE))%>%arrange(desc(total_won))
loser_countries_rank
## # A tibble: 93 x 2
## flag_code total_won
## <chr> <dbl>
## 1 AUS 1989
## 2 CZE 1209
## 3 CAN 1190
## 4 SVK 889
## 5 BRA 873
## 6 POR 621
## 7 RSA 566
## 8 KAZ 495
## 9 KOR 438
## 10 GEO 377
## # … with 83 more rows
ANALYSIS-1: Youngest Winner her/his birth date and name of tournament that s/he won
winner_tourney_match_df<-left_join(tourney_df,player_df,by=c("singles_winner_player_id"="player_id"))
sorted_by_desc_birth_date_winner_df<-winner_tourney_match_df%>%select(first_name,last_name,birth_date,tourney_name)%>%arrange(desc(birth_date))
youngest_winner_birth_date_tourney<-sorted_by_desc_birth_date_winner_df[1,]
youngest_winner_birth_date_tourney
## # A tibble: 1 x 4
## first_name last_name birth_date tourney_name
## <chr> <chr> <date> <chr>
## 1 Andrey Rublev 1997-10-20 Plava Laguna Croatia Open Umag
ANALYSIS-2: Best Player of 2017 and Number of succeeded tournaments by her/him
success_df<-score_df%>%select(tourney_id,tourney_round_name,winner_player_id)%>%filter(tourney_round_name=="Finals")%>%count(winner_player_id,sort = TRUE)
best_player_2007<-player_df%>%left_join(.,success_df,by=c("player_id"="winner_player_id"))%>%select(first_name,last_name,n)%>%arrange(desc(n))%>%slice(1)
best_player_2007
## # A tibble: 1 x 3
## first_name last_name n
## <chr> <chr> <int>
## 1 Roger Federer 7