2017-03-04

Introduction

In this post I create RMarkdown templates for end-to-end analysis of IPL T20 matches, that are available on Cricsheet based on my R package yorkr.  With these templates you can convert all IPL data which is in yaml format to R dataframes. Further I create data and the necessary templates for analyzing IPL matches, teams and players. All of these can be accessed at yorkrIPLTemplate. The templates are

Template for conversion and setup – IPLT20Template.Rmd

Any IPL match – IPLMatchtemplate.Rmd

IPL matches between 2 nations – IPLMatches2TeamTemplate.Rmd

A IPL nations performance against all other IPL nations – IPLAllMatchesAllOppnTemplate.Rmd

Analysis of IPL batsmen and bowlers of all IPL nations – IPLBatsmanBowlerTemplate.Rmd

Besides the templates the repository also includes the converted data for all IPL matches I downloaded from Cricsheet in Dec 2016. So this data is complete till the 2016 IPL season. You can recreate the files as more matches are added to Cricsheet site in IPL 2017 and future seasons. This post contains all the steps needed for detailed analysis of IPL matches, teams and IPL player. This will also be my reference in future if I decide to analyze IPL in future!

There will be 5 folders at the root

IPLdata – Match files as yaml from Cricsheet

IPLMatches – Yaml match files converted to dataframes

IPLMatchesBetween2Teams – All Matches between any 2 IPL teams

allMatchesAllOpposition – An IPL teams’s performance against all other teams

BattingBowlingDetails – Batting and bowling details of all IPL teams

The first few steps take care of the data setup. This needs to be done before any of the analysis of IPL batsmen, bowlers, any IPL match, matches between any 2 IPL countries or analysis of a teams performance against all other countries

There will be 5 folders at the root

data

IPLMatches

IPLMatchesBetween2Teams

allMatchesAllOpposition

BattingBowlingDetails

The source YAML files will be in IPLData folder

1.Create directory of IPLMatches

Some files may give conversions errors. You could try to debug the problem or just remove it from the IPLdata folder. At most 2-4 file will have conversion problems and I usally remove then from the files to be converted.

Also take a look at my GooglyPlus shiny app which was created after performing the same conversion on the Dec 16 data .

2.Save all matches between all combinations of IPL nations

This function will create the set of all matches between each IPL team against every other IPL team. This uses the data that was created in IPLMatches, with the convertAllYaml2RDataframesIPL() function.

3.Save all matches against all opposition

This will create a consolidated dataframe of all matches played by every IPL playing nation against all other nattions. This also uses the data that was created in IPLMatches, with the convertAllYaml2RDataframesIPL() function.

4. Create batting and bowling details for each IPL team

These are the current IPL playing teams. You can add to this vector as newer IPL teams start playing IPL. You will get to know all IPL teams by also look at the directory created above namely allMatchesAllOpposition. This also uses the data that was created in IPLMatches, with the convertAllYaml2RDataframesIPL() function.

5. Get the list of batsmen for a particular IPL team

The following code is needed for analyzing individual IPL batsmen. In IPL a player could have played in multiple IPL teams.

6. Get the list of bowlers for a particular IPL team

The method below can get the list of bowler names for any IPL team.The following code is needed for analyzing individual IPL bowlers. In IPL a player could have played in multiple IPL teams.

Now we are all set

A)  IPL T20 Match Analysis

1 IPL Match Analysis

Load any match data from the ./IPLMatches folder for e.g. Chennai Super Kings-Deccan Chargers-2008-05-06.RData

All analysis for this match can be done now

2. Scorecard

3.Batting Partnerships

4. Batsmen vs Bowler Plot

5. Team bowling scorecard

6. Team bowling Wicket kind match

7. Team Bowling Wicket Runs Match

8. Team Bowling Wicket Match

9. Team Bowler vs Batsmen

10. Match Worm chart

B)  IPL  Matches between 2  IPL teams

1 IPL Match Analysis

Load any match data from the ./IPLMatches folder for e.g. Chennai Super Kings-Deccan Chargers-2008-05-06.RData

All analysis for this match can be done now

2. Scorecard

3.Batting Partnerships

4. Batsmen vs Bowler Plot

5. Team bowling scorecard

6. Team bowling Wicket kind match

7. Team Bowling Wicket Runs Match

8. Team Bowling Wicket Match

9. Team Bowler vs Batsmen

10. Match Worm chart

C)  IPL Matches for a team against all other teams

1. IPL Matches for a team against all other teams

Load the data between for a IPL team against all other countries ./allMatchesAllOpposition for e.g all matches of Kolkata Knight Riders

2. Team’s batting scorecard all Matches

3. Batting scorecard of opposing team

4. Team batting partnerships

5. Team batting partnerships plot

6, Team batsmen vs bowlers report

7. Team batsmen vs bowler plot

8. Team bowling scorecard

9. Team bowler vs batsmen

10. Team Bowler vs bastmen

11. Team bowler wicket kind

12.

1 IPL Batsman setup functions

Get the batsman’s details for a batsman

setwd("../BattingBowlingDetails")
# IPL Team names
IPLTeamNames <- list("Chennai Super Kings","Deccan Chargers", "Delhi Daredevils","Kings Xi Punjab",
"Kochi Tuskers Kerala","Kolkata Knight Riders","Mumbai Indians","Pune Warriors",
"Rajasthan Royals","Royal Challengers Bangalore","Sunrisers Hyderabad","Gujarat Lions",
"Rising Pune Supergiants")

# Check and get the team indices of IPL teams in which the batsman has played
getTeamIndex <- function(batsman){
setwd("./BattingBowlingDetails")
load("csk.RData")
load("dc.RData")
load("dd.RData")
load("kxip.RData")
load("ktk.RData")
load("kkr.RData")
load("mi.RData")
load("pw.RData")
load("rr.RData")
load("rcb.RData")
load("sh.RData")
load("gl.RData")
load("rps.RData")
setwd("..")
getwd()
print(ls())
teams_batsmen = list(csk_batsmen,dc_batsmen,dd_batsmen,kxip_batsmen,ktk_batsmen,kkr_batsmen,mi_batsmen,
pw_batsmen,rr_batsmen,rcb_batsmen,sh_batsmen,gl_batsmen,rps_batsmen)
b <- NULL
for (i in 1:length(teams_batsmen)){
a <- which(teams_batsmen[[i]] == batsman)

if(length(a) != 0)
b <- c(b,i)
}
b
}

# Get the list of the IPL team names from the indices passed
getTeams <- function(x){

l <- NULL
# Get the teams passed in as indexes
for (i in seq_along(x)){

l <- c(l, IPLTeamNames[[x[i]]])

}
l
}

# Create a consolidated data frame with all teams the IPL batsman has played for
getIPLBatsmanDF <- function(teamNames){
batsmanDF <- NULL
# Create a consolidated Data frame of batsman for all IPL teams played
for (i in seq_along(teamNames)){
df <- getBatsmanDetails(team=teamNames[i],name=IPLBatsman,dir="./BattingBowlingDetails")
batsmanDF <- rbind(batsmanDF,df)

Show more