Library

library(tidyverse)

Exploring Data Frames

url<-url("https://github.com/pjournal/mef03-ozgurken/blob/master/atp_tennis_data_2017.RData?raw=true")
atp_tennis<- load(url)

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(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" ...
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(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 ...

1. Countries Based on Their Championship (Rank countries (flag codes) by the number of singles champions)

inner_join(player_df, tourney_df, by = c("player_id" = "singles_winner_player_id")) %>%
  select(player_id, flag_code) %>% group_by(flag_code) %>% summarise(winner_count = n()) %>% arrange(desc(winner_count))
## # A tibble: 21 x 2
##    flag_code winner_count
##    <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

2. Rank countries which did not get any singles champs by the games won when they win the match

raw_data <- inner_join(player_df, tourney_df, by = c("player_id" = "singles_winner_player_id")) %>%
  select(player_id, flag_code) %>% group_by(flag_code) %>% summarise(winner_count = n()) %>% arrange(desc(winner_count))

loser_countries <- anti_join(player_df, raw_data) %>% select(flag_code) %>% distinct()
inner_join(player_df, loser_countries) %>% select(player_id, flag_code) %>% distinct() %>%
  inner_join(score_df, by = c("player_id" = "winner_player_id")) %>% select(flag_code, winner_games_won) %>%
  group_by(flag_code) %>% transmute(winner_games_won = sum(winner_games_won)) %>%distinct() %>%
  arrange(desc(winner_games_won))
## # A tibble: 39 x 2
## # Groups:   flag_code [39]
##    flag_code winner_games_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 29 more rows

3. Rank Players by their Weight that won Single Winnings in matches

 singles_winners <- tourney_df %>% 
  left_join(player_df, by = c("singles_winner_player_id" = "player_id")) %>%
  select(weight_kg, singles_winner_player_id)

singles_winners$weight_kg <- as.numeric(singles_winners$weight_kg)


singles_winners %>% group_by(weight_kg) %>% count(weight_kg, sort = TRUE)
## # A tibble: 22 x 2
## # Groups:   weight_kg [22]
##    weight_kg     n
##        <dbl> <int>
##  1        85    13
##  2        80     6
##  3        86     6
##  4        84     5
##  5        70     4
##  6        81     4
##  7        91     4
##  8        68     3
##  9        77     3
## 10        89     3
## # ... with 12 more rows

4. Rank Players by their Age based on Singles Winning. Eliminate Repeating Years to find the Oldest Age

win_join <-inner_join(player_df, tourney_df, by = c("player_id" = "singles_winner_player_id")) %>% select(player_id,first_name,last_name)


age_join<-inner_join(win_join, rank_df, by = c("player_id" = "player_id"))

age_join <- age_join %>% arrange(desc(player_age)) %>% distinct(first_name,last_name,player_age)

age_data <- age_join %>%mutate(player_name=paste(first_name," ",last_name),rank =row_number(),vars_group = 'player_age') %>% filter(rank<17) %>% select(player_name,player_age) %>% distinct(player_name, player_age) %>% select(player_name, player_age) %>% group_by(player_name) %>% filter(player_age == max(player_age))

age_data           
## # A tibble: 10 x 2
## # Groups:   player_name [10]
##    player_name              player_age
##    <chr>                         <dbl>
##  1 Victor   Estrella Burgos         37
##  2 Roger   Federer                  36
##  3 Feliciano   Lopez                36
##  4 David   Ferrer                   35
##  5 Philipp   Kohlschreiber          34
##  6 Gilles   Muller                  34
##  7 John   Isner                     32
##  8 Jo-Wilfried   Tsonga             32
##  9 Stan   Wawrinka                  32
## 10 Pablo   Cuevas                   31