yorkr crashes the IPL party ! – Part 1

[This article was first published on R – Giga thoughts …, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

 

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')

batsmenPartnership-1

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

batsmenPartnership-2

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

batsmenPartnership-3

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)

batsmenPartnership-4

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)

batsmenVsBowler-1

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

batsmenVsBowler-2

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")

batsmenVsBowler-3

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

batsmenVsBowler-4

10. Bowling Scorecard

This function provides the bowling performance, the number of overs bowled, maidens, runs conceded and wickets taken for each match

teamBowlingScorecardMatch(kkr_mi,'Kolkata Knight Riders')
## Source: local data frame [6 x 5]
## 
##            bowler overs maidens  runs wickets
##            (fctr) (int)   (int) (dbl)   (dbl)
## 1        M Morkel     4       0    35       2
## 2        UT Yadav     3       0    24       0
## 3 Shakib Al Hasan     4       0    21       1
## 4       SP Narine     4       0    18       1
## 5       PP Chawla     4       0    32       1
## 6       YK Pathan     1       0    10       0
teamBowlingScorecardMatch(kkr_mi,'Mumbai Indians')
## Source: local data frame [6 x 5]
## 
##            bowler overs maidens  runs wickets
##            (fctr) (int)   (int) (dbl)   (dbl)
## 1      SL Malinga     4       0    30       1
## 2       JJ Bumrah     3       0    23       0
## 3 Harbhajan Singh     4       0    22       2
## 4         PP Ojha     4       0    25       0
## 5     LMP Simmons     3       0    34       1
## 6      KA Pollard     1       0     7       0
teamBowlingScorecardMatch(sh_rcb,"Sunrisers Hyderabad")
## Source: local data frame [7 x 5]
## 
##            bowler overs maidens  runs wickets
##            (fctr) (int)   (int) (dbl)   (dbl)
## 1         B Kumar     4       0    27       2
## 2        DW Steyn     4       0    23       1
## 3   Parvez Rasool     4       0    26       1
## 4       KV Sharma     3       0    27       1
## 5 Y Venugopal Rao     1       0     7       0
## 6       IK Pathan     3       0    28       1
## 7       DJG Sammy     1       0    19       0
teamBowlingScorecardMatch(rr_pw,'Pune Warriors')
## Source: local data frame [6 x 5]
## 
##         bowler overs maidens  runs wickets
##         (fctr) (int)   (int) (dbl)   (dbl)
## 1      B Kumar     4       0    38       1
## 2   K Upadhyay     3       0    29       0
## 3   WD Parnell     4       0    27       3
## 4     R Sharma     4       0    38       0
## 5 Yuvraj Singh     2       0    16       0
## 6   AD Mathews     3       0    34       1
teamBowlingScorecardMatch(dc_csk,"Chennai Super Kings")
## Source: local data frame [5 x 5]
## 
##           bowler overs maidens  runs wickets
##           (fctr) (int)   (int) (dbl)   (int)
## 1        M Ntini     4       0    24       1
## 2        MS Gony     4       0    21       1
## 3      JA Morkel     4       0    37       3
## 4 M Muralitharan     4       0    22       1
## 5       L Balaji     4       0    34       2
teamBowlingScorecardMatch(kxp_dd,"Kings XI Punjab")
## Source: local data frame [5 x 5]
## 
##            bowler overs maidens  runs wickets
##            (fctr) (int)   (int) (dbl)   (int)
## 1         P Awana     3       1    15       2
## 2        AR Patel     4       0    28       2
## 3      MG Johnson     4       1    27       2
## 4 Karanveer Singh     4       0    22       2
## 5        R Dhawan     4       0    22       2

11. Wicket Kind

The plots below provide the bowling kind of wicket taken by the bowler (caught, bowled, lbw etc.)

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

bowlingWicketKind-1

m <- teamBowlingWicketKindMatch(rr_pw,'Pune Warriors','Rajasthan Royals',plot=FALSE)
m
##         bowler wicketKind wicketPlayerOut runs
## 1   AD Mathews     caught        R Dravid   34
## 2   WD Parnell     bowled       SR Watson   27
## 3      B Kumar     caught       AM Rahane   38
## 4   WD Parnell     caught        BJ Hodge   27
## 5   WD Parnell     caught       SV Samson   27
## 6   K Upadhyay   noWicket        noWicket   29
## 7     R Sharma   noWicket        noWicket   38
## 8 Yuvraj Singh   noWicket        noWicket   16
teamBowlingWicketKindMatch(dc_csk,"Chennai Super Kings","Deccan Chargers")

bowlingWicketKind-2

teamBowlingWicketKindMatch(kxp_dd,"Kings XI Punjab","Delhi Daredevils",plot=TRUE)

bowlingWicketKind-3

teamBowlingWicketKindMatch(sh_rcb,"Royal Challengers Bangalore","Sunrisers Hyderabad")

bowlingWicketKind-4

12. Wicket vs Runs conceded

The plots below provide the wickets taken and the runs conceded by the bowler in the match

teamBowlingWicketRunsMatch(dc_csk,"Deccan Chargers", "Chennai Super Kings")

wicketRuns-1

teamBowlingWicketRunsMatch(kxp_dd,"Kings XI Punjab","Delhi Daredevils",plot=TRUE)

wicketRuns-2

teamBowlingWicketRunsMatch(sh_rcb,"Sunrisers Hyderabad","Royal Challengers Bangalore")

wicketRuns-3

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

wicketRuns-4

m <- teamBowlingWicketKindMatch(rr_pw,'Pune Warriors','Rajasthan Royals',plot=FALSE)
m
##         bowler wicketKind wicketPlayerOut runs
## 1   AD Mathews     caught        R Dravid   34
## 2   WD Parnell     bowled       SR Watson   27
## 3      B Kumar     caught       AM Rahane   38
## 4   WD Parnell     caught        BJ Hodge   27
## 5   WD Parnell     caught       SV Samson   27
## 6   K Upadhyay   noWicket        noWicket   29
## 7     R Sharma   noWicket        noWicket   38
## 8 Yuvraj Singh   noWicket        noWicket   16

13. Wickets taken by bowler

The plots provide the wickets taken by the bowler

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

bowlingWickets-1

m <- teamBowlingWicketMatch(rr_pw,'Pune Warriors','Rajasthan Royals',plot=FALSE)
m
##         bowler wicketKind wicketPlayerOut runs
## 1   AD Mathews     caught        R Dravid   34
## 2   WD Parnell     bowled       SR Watson   27
## 3      B Kumar     caught       AM Rahane   38
## 4   WD Parnell     caught        BJ Hodge   27
## 5   WD Parnell     caught       SV Samson   27
## 6   K Upadhyay   noWicket        noWicket   29
## 7     R Sharma   noWicket        noWicket   38
## 8 Yuvraj Singh   noWicket        noWicket   16
teamBowlingWicketMatch(sh_rcb,"Royal Challengers Bangalore","Sunrisers Hyderabad")

bowlingWickets-2

teamBowlingWicketMatch(dc_csk,"Deccan Chargers", "Chennai Super Kings")

bowlingWickets-3

teamBowlingWicketMatch(kxp_dd,"Kings XI Punjab","Delhi Daredevils",plot=TRUE)

bowlingWickets-4

14. Bowler Vs Batsmen

The functions compute and display how the different bowlers of the country performed against the batting opposition.

teamBowlersVsBatsmenMatch(dc_csk,"Deccan Chargers", "Chennai Super Kings")

bowlerVsBatsmen-1

teamBowlersVsBatsmenMatch(kxp_dd,"Kings XI Punjab","Delhi Daredevils",plot=TRUE)

bowlerVsBatsmen-2

m <-teamBowlersVsBatsmenMatch(sh_rcb,"Sunrisers Hyderabad","Royal Challengers Bangalore",plot=FALSE)
m
## Source: local data frame [26 x 3]
## Groups: bowler [?]
## 
##      bowler        batsman runsConceded
##      (fctr)         (fctr)        (dbl)
## 1   B Kumar       CH Gayle            5
## 2   B Kumar       PA Patel            4
## 3   B Kumar        V Kohli            6
## 4   B Kumar AB de Villiers            6
## 5   B Kumar         S Rana            1
## 6   B Kumar       MA Starc            5
## 7  DW Steyn       CH Gayle            7
## 8  DW Steyn        V Kohli            4
## 9  DW Steyn AB de Villiers            4
## 10 DW Steyn         S Rana            7
## ..      ...            ...          ...
teamBowlersVsBatsmenMatch(rr_pw,'Pune Warriors','Rajasthan Royals')

bowlerVsBatsmen-3

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

bowlerVsBatsmen-4

15. Match worm graph

The plots below provide the match worm graph for the IPL Twenty 20 matches

matchWormGraph(dc_csk,"Deccan Chargers", "Chennai Super Kings")

matchWorm-1

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

matchWorm-2

matchWormGraph(sh_rcb,"Sunrisers Hyderabad","Royal Challengers Bangalore")

matchWorm-3

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

matchWorm-4

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

matchWorm-5

Conclusion

This post included all functions between 2 IPL teams from the package yorkr for IPL Twenty20 matches.As mentioned above the yaml match files have been already converted to dataframes and are available for download from Github. Go ahead and give it a try

To be continued. Watch this space!

You may also like

  1. Introducing cricket package yorkr-Part1:Beaten by sheer pace!.
  2. Introducing cricketr! : An R package to analyze performances of cricketers
  3. Simulating a Web Joint in Android
  4. Elements of CRUD with NodeExpress and MongoDB using Enide Studio
  5. Cricket analytics with cricketr
  6. Sixer – R package cricketr’s new Shiny avatar
  7. Natural language processing: What would Shakespeare say?
  8. Experiment with deblurring using OpenCV
  9. Presentation on Wireless Technologies – Part 2

To leave a comment for the author, please follow the link and comment on their blog: R – Giga thoughts ….

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)