library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
dt<- load('C:/Users/win7pro/Desktop/BDA CODES/BDA503 - R/atp_tennis_data_2017.Rdata')
#Rank countries (flag cıdes) by the number of singles champions

play_rank <- right_join(player_df, tourney_df, by = c('player_id'='singles_winner_player_id'))

play_rank %>% group_by(flag_code) %>% count(sort = TRUE) 
#Rank countries which did not get any singles championships by the games won when they win the match

nonchamp_players <- player_df %>% select(player_id,flag_code) %>% anti_join(.,play_rank, by="player_id")

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))
#Which countries won the maximum games by tourney conditions?

play_rank <- right_join(player_df, tourney_df, by = c('player_id'='singles_winner_player_id'))

play_rank %>% group_by(flag_code,tourney_conditions) %>% count(sort=TRUE)
#Which player won the maximum point at 2017

first_week <- inner_join(rank_df,player_df) %>% filter(week_title=="2017-01-02")
## Joining, by = "player_id"
last_week <- inner_join(rank_df,player_df) %>% filter(week_title=="2017-11-20")
## Joining, by = "player_id"
inner_join(first_week,last_week,by="player_id",suffix=c("_first","_last")) %>% transmute(first_name_first,last_name_first,flag_code_first,tourneys_played_first,point=ranking_points_last - ranking_points_first) %>% arrange(desc(point))