Where tireless striving stretches its arms towards perfection

Where the clear stream of reason has not lost its way

Into the dreary desert sand of dead habit

                Rabindranath Tagore

Introduction

In this post, my R package yorkr crashes the IPL party! In my earlier posts I had already created functions for handling Twenty20 matches. I now use these functions to analyze the IPL T20 matches. This package is based on data from Cricsheet. The T20 functionality were added in the following posts

  1. yorkr pads up for the Twenty20s: Part 1- Analyzing team“s match performance.
  2. yorkr pads up for the Twenty20s: Part 2-Head to head confrontation between teams
  3. yorkr pads up for the Twenty20s:Part 3:Overall team performance against all oppositions!
  4. yorkr pads up for the Twenty20s:Part 4- Individual batting and bowling performances

The yorkr package provides functions to convert the yaml files to more easily R consumable entities, namely dataframes. All converted files for ODI,T20 and IPL are available for use at yorkrData.

The IPL T20 matches can be downloaded from IPL-T20-matches

This post can be viewed at RPubs at yorkrIPLT20-Part1 or can also be downloaded as a PDF document yorkrIPLT20-1.pdf

2. Install the package from CRAN

library(yorkr)
rm(list=ls())

2a. New functionality for Twenty20

The functions that were used to convert the Twenty20 yaml files to RData are

  1. convertYaml2RDataframeT20
  2. convertAllYaml2RDataframesT20

Note 1: While I have already converted the IPL T20 files, you will need to use these functions for future IPL matches

Note 2: This post includes some cosmetic changes made over yorkr_0.0.4, where I make the plot title more explicit. The functionality will be available in a few weeks from now in yorkr_0.0.5

3. Convert and save T20 yaml file to dataframe

This function will convert a T20 IPL yaml file, in the format as specified in Cricsheet to dataframe. This will be saved as as RData file in the target directory. The name of the file wil have the following format team1-team2-date.RData. An example of how a yaml file can be converted to a dataframe and saved is shown below.

convertYaml2RDataframeT20("335982.yaml",".",".") 
## [1] "./335982.yaml"
## [1] "first loop"
## [1] "second loop"

4. Convert and save all T20 yaml files to dataframes

This function will convert all IPL T20 yaml files from a source directory to dataframes, and save it in the target directory, with the names as mentioned above. Since I have already done this, I will not be executing this again. You can download the zip of all the converted RData files from Github at IPL-T20-matches

#convertAllYaml2RDataframesT20("./IPL","./data")

5. yorkrData - A Github repositiory

Cricsheet had a total of 518 IPL Twenty20 matches. Out of which 9 files seemed to have problem. The remaining 509 T20 matches have been converted to RData.

All the converted RData files can be accessed from my Github link yorkrData under the folder IPL-T20-matches

You can download the the zip of the files and use it directly in the functions as follows

6. Load the match data as dataframes

For this post I will be using the IPL Twenty20 match data from 5 random matches between 10 different opposing IPL teams. For this I will directly use the converted RData files rather than getting the data through the getMatchDetails() as shown below

With the RData we can load the data in 2 ways

A. With getMatchDetails()

  1. With getMatchDetails() using the 2 teams and the date on which the match occured
sh_mi <- getMatchDetails("Sunrisers Hyderabad","Royal Challengers Bangalore","2014-05-20",dir=".")
dim(sh_mi)
## [1] 244  25

or

B.Directly load RData into your code.

The match details will be loaded into a dataframe called ’overs’ which you can assign to a suitable name as below

The randomly selected IPL T20 matches are

  • Sunrisers Hyderabad vs Royal Challengers Bangalore, 2014-05-20
  • Rajasthan Royals vs Pune Warriors, 2013-05-05
  • Deccan Chargers vs Chennai Super Kings, 2008-05-27
  • Kings Xi Punjab vs Delhi Daredevils, 2014-05-25
  • Kolkata Knight Riders vs Mumbai Indian, 2014-05-14
setwd("C:/software/cricket-package/cricsheet/cleanup/IPL/part1")
load("Sunrisers Hyderabad-Royal Challengers Bangalore-2014-05-20.RData")
sh_rcb <- overs
load("Rajasthan Royals-Pune Warriors-2013-05-05.RData")
rr_pw <- overs
load("Deccan Chargers-Chennai Super Kings-2008-05-27.RData")
dc_csk <- overs
load("Kings XI Punjab-Delhi Daredevils-2014-05-25.RData")
kxp_dd <-overs
load("Kolkata Knight Riders-Mumbai Indians-2014-05-14.RData")
kkr_mi <- overs

7. Team batting scorecard

Compute and display the batting scorecard of the teams in the match.

teamBattingScorecardMatch(kkr_mi,'Mumbai Indians')
## Total= 134
## Source: local data frame [7 x 5]
## 
##       batsman ballsPlayed fours sixes  runs
##        (fctr)       (int) (dbl) (dbl) (dbl)
## 1 LMP Simmons          13     2     0    12
## 2   CM Gautam           9     1     0     8
## 3   AT Rayudu          26     3     1    33
## 4   RG Sharma          45     4     2    51
## 5 CJ Anderson          12     1     1    18
## 6  KA Pollard          11     0     0    10
## 7     AP Tare           3     0     0     2
teamBattingScorecardMatch(kkr_mi,'Kolkata Knight Riders')
## Total= 137
## Source: local data frame [5 x 5]
## 
##           batsman ballsPlayed fours sixes  runs
##            (fctr)       (int) (dbl) (dbl) (dbl)
## 1      RV Uthappa          52     9     3    80
## 2       G Gambhir          17     1     0    14
## 3       MK Pandey          21     0     0    14
## 4       YK Pathan          13     3     0    20
## 5 Shakib Al Hasan           8     1     0     9
teamBattingScorecardMatch(sh_rcb,'Sunrisers Hyderabad')
## Total= 154
## Source: local data frame [5 x 5]
## 
##     batsman ballsPlayed fours sixes  runs
##      (fctr)       (int) (dbl) (dbl) (dbl)
## 1  S Dhawan          39     7     1    50
## 2 DA Warner          43     3     4    59
## 3   NV Ojha          19     0     2    24
## 4  AJ Finch           9     1     0    11
## 5 DJG Sammy           4     0     1    10
teamBattingScorecardMatch(rr_pw,'Pune Warriors')
## Total= 167
## Source: local data frame [5 x 5]
## 
##        batsman ballsPlayed fours sixes  runs
##         (fctr)       (int) (int) (dbl) (dbl)
## 1   RV Uthappa          41     8     1    54
## 2     AJ Finch          32     7     0    45
## 3 Yuvraj Singh          11     1     1    15
## 4     MR Marsh          21     2     3    35
## 5   AD Mathews          15     2     0    18
teamBattingScorecardMatch(dc_csk,'Chennai Super Kings')
## Total= 137
## Source: local data frame [5 x 5]
## 
##      batsman ballsPlayed fours sixes  runs
##       (fctr)       (int) (int) (dbl) (dbl)
## 1   PA Patel          27     3     0    20
## 2 SP Fleming           9     3     0    14
## 3   SK Raina          41     5     2    54
## 4   MS Dhoni          24     4     1    37
## 5  JA Morkel          12     1     0    12
teamBattingScorecardMatch(kxp_dd,'Kings XI Punjab')
## Total= 104
## Source: local data frame [5 x 5]
## 
##      batsman ballsPlayed fours sixes  runs
##       (fctr)       (int) (dbl) (dbl) (dbl)
## 1   V Sehwag           7     2     0     9
## 2    M Vohra          37     4     2    47
## 3 GJ Maxwell           2     0     0     0
## 4  DA Miller          34     4     2    47
## 5  GJ Bailey           1     0     0     1
teamBattingScorecardMatch(kkr_mi,'Mumbai Indians')
## Total= 134
## Source: local data frame [7 x 5]
## 
##       batsman ballsPlayed fours sixes  runs
##        (fctr)       (int) (dbl) (dbl) (dbl)
## 1 LMP Simmons          13     2     0    12
## 2   CM Gautam           9     1     0     8
## 3   AT Rayudu          26     3     1    33
## 4   RG Sharma          45     4     2    51
## 5 CJ Anderson          12     1     1    18
## 6  KA Pollard          11     0     0    10
## 7     AP Tare           3     0     0     2

8. Plot the team batting partnerships

The functions below plot the team batting partnership in the match Note: Many of the plots include an additional parameters plot which is either TRUE or FALSE. The default value is plot=TRUE. When plot=TRUE the plot will be displayed. When plot=FALSE the data frame will be returned to the user. The user can use this to create an interactive chary using one of the packages like rcharts, ggvis,googleVis or plotly.

teamBatsmenPartnershipMatch(kkr_mi,'Mumbai Indians','Kolkata Knight Riders')

teamBatsmenPartnershipMatch(sh_rcb,'Sunrisers Hyderabad','Royal Challengers Bangalore',plot=TRUE)

teamBatsmenPartnershipMatch(rr_pw,'Pune Warriors','Rajasthan Royals')

teamBatsmenPartnershipMatch(dc_csk,'Chennai Super Kings','Deccan Chargers',plot=FALSE)
##      batsman nonStriker runs
## 1   PA Patel SP Fleming   10
## 2   PA Patel   SK Raina   10
## 3 SP Fleming   PA Patel   14
## 4   SK Raina   PA Patel   19
## 5   SK Raina   MS Dhoni   14
## 6   SK Raina  JA Morkel   21
## 7   MS Dhoni   SK Raina   37
## 8  JA Morkel   SK Raina   12
teamBatsmenPartnershipMatch(kxp_dd,'Kings XI Punjab','Delhi Daredevils',plot=TRUE)

9. Batsmen vs Bowler

The function below computes and plots the performances of the batsmen vs the bowlers. As before the plot parameter can be set to TRUE or FALSE. By default it is plot=TRUE

teamBatsmenVsBowlersMatch(sh_rcb,"Sunrisers Hyderabad","Royal Challengers Bangalore", plot=TRUE)

teamBatsmenVsBowlersMatch(kkr_mi,'Kolkata Knight Riders','Mumbai Indians')

m <- teamBatsmenVsBowlersMatch(rr_pw,'Pune Warriors','Rajasthan Royals',plot=FALSE)
m
## Source: local data frame [20 x 3]
## Groups: batsman [?]
## 
##         batsman      bowler runsConceded
##          (fctr)      (fctr)        (dbl)
## 1    RV Uthappa  A Chandila           12
## 2    RV Uthappa JP Faulkner            1
## 3    RV Uthappa   SR Watson           13
## 4    RV Uthappa   KK Cooper            2
## 5    RV Uthappa  SK Trivedi           18
## 6    RV Uthappa   STR Binny            8
## 7      AJ Finch  A Chandila           11
## 8      AJ Finch JP Faulkner           12
## 9      AJ Finch   SR Watson            5
## 10     AJ Finch   KK Cooper            8
## 11     AJ Finch  SK Trivedi            9
## 12 Yuvraj Singh   KK Cooper            0
## 13 Yuvraj Singh  SK Trivedi            5
## 14 Yuvraj Singh   STR Binny           10
## 15     MR Marsh JP Faulkner           13
## 16     MR Marsh   SR Watson            7
## 17     MR Marsh   KK Cooper           15
## 18   AD Mathews JP Faulkner            7
## 19   AD Mathews   SR Watson            3
## 20   AD Mathews   KK Cooper            8
teamBatsmenVsBowlersMatch(dc_csk,"Chennai Super Kings","Deccan Chargers")

teamBatsmenVsBowlersMatch(kxp_dd,"Kings XI Punjab","Delhi Daredevils")