yorkr crashes the IPL party! – Part 4

[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.

Introduction

I’ve missed more than 9000 shots in my career. I’ve lost almost 300 games. 26 times, I’ve been trusted to take the game winning shot and missed. I’ve failed over and over and over again in my life. And that is why I succeed.

                      Michael Jordan

Success is where preparation and opportunity meet.

                      Bobby Unser

It is not whether you get knocked down. It is whether you get up.

                      Vince Lombardi

Make sure your worst enemy doesn’t live between your own two ears.

                      Laird Hamilton

This post should be the last post for “yorkr crashes the IPL party!”. In fact it is final post for the whole ‘yorkr’ series. I have now covered the use of yorkr for ODIs, Twenty20s and IPL T20 formats. I will not be including functionality in yorkr to handle Test cricket from Cricsheet. I would recommend that you use my R package cricketr. Please see my post Introducing cricketr! : An R package to analyze performances of cricketers

In this last post on IPL T20 I look at the top individual batting and bowling performances in the IPL Twenty20s. Also please take a look at my 3 earlier post on yorkr’s handling of IPL Twenty20 matches

  1. yorkr crashes the IPL party ! – Part 1
  2. yorkr crashes the IPL party ! – Part 2
  3. yorkr crashes the IPL party ! – Part 3

This post has also been published at RPubs IPLT20-Part4 and can also be downloaded as a PDF document from IPLT20-Part4.pdf.

You can clone/fork the code for the package yorkr from Github at yorkr-package

Take a look at my book with all my articles related to yorkr now available at Amazon in paperback and Kindle formats  Beaten by sheer pace! Cricket analytics with yorkr. The book is also available at Leanpub, which has a variable pricing Beaten by sheer pace! Cricket analytics with yorkr.

The list of Class 4 functions are shown below.The Twenty20 features will be available from yorkr_0.0.4

Batsman functions

  1. batsmanRunsVsDeliveries
  2. batsmanFoursSixes
  3. batsmanDismissals
  4. batsmanRunsVsStrikeRate
  5. batsmanMovingAverage
  6. batsmanCumulativeAverageRuns
  7. batsmanCumulativeStrikeRate
  8. batsmanRunsAgainstOpposition
  9. batsmanRunsVenue
  10. batsmanRunsPredict

Bowler functions

  1. bowlerMeanEconomyRate
  2. bowlerMeanRunsConceded
  3. bowlerMovingAverage
  4. bowlerCumulativeAvgWickets
  5. bowlerCumulativeAvgEconRate
  6. bowlerWicketPlot
  7. bowlerWicketsAgainstOpposition
  8. bowlerWicketsVenue
  9. bowlerWktsPredict
library(yorkr)
library(gridExtra)
library(rpart.plot)
library(dplyr)
library(ggplot2)
rm(list=ls())

A. Batsman functions

1. Get IPL Team Batting details

The function below gets the overall IPL team batting details based on the RData file available in IPL T20 matches. This is currently also available in Github at [IPL-T20-matches] (https://github.com/tvganesh/yorkrData/tree/master/IPL/IPL-T20-matches). The batting details of the IPL team in each match is created and a huge data frame is created by rbinding the individual dataframes. This can be saved as a RData file

setwd("C:/software/cricket-package/york-test/yorkrData/IPL/IPL-T20-matches")
csk_details <- getTeamBattingDetails("Chennai Super Kings",dir=".", save=TRUE)
dd_details <- getTeamBattingDetails("Delhi Daredevils",dir=".",save=TRUE)
kkr_details <- getTeamBattingDetails("Kolkata Knight Riders",dir=".",save=TRUE)
mi_details <- getTeamBattingDetails("Mumbai Indians",dir=".",save=TRUE)
rcb_details <- getTeamBattingDetails("Royal Challengers Bangalore",dir=".",save=TRUE)

2. Get IPL batsman details

This function is used to get the individual IPL T20 batting record for a the specified batsman of the team as in the functions below. For analyzing the batting performances I have chosen the top IPL T20 batsmen from the teams. This was based to a large extent on batting scorecard functions from yorkr crashes the IPL party!:Part 3 The top IPL batsmen chosen are the ones below

  1. Suresh Raina (CSK)
  2. MS Dhoni (CSK)
  3. Virendar Sehwag (DD)
  4. Rohit Sharma (MI)
  5. Gautham Gambhir (KKR)
  6. Virat Kohli (RCB)
setwd("C:/software/cricket-package/cricsheet/cleanup/IPL/part4")
raina <- getBatsmanDetails(team="Chennai Super Kings",name="SK Raina",dir=".")
## [1] "./Chennai Super Kings-BattingDetails.RData"
dhoni <- getBatsmanDetails(team="Chennai Super Kings",name="MS Dhoni")
## [1] "./Chennai Super Kings-BattingDetails.RData"
sehwag <-  getBatsmanDetails(team="Delhi Daredevils",name="V Sehwag",dir=".")
## [1] "./Delhi Daredevils-BattingDetails.RData"
gambhir <-  getBatsmanDetails(team="Kolkata Knight Riders",name="G Gambhir",dir=".")
## [1] "./Kolkata Knight Riders-BattingDetails.RData"
rsharma <-  getBatsmanDetails(team="Mumbai Indians",name="RG Sharma",dir=".")
## [1] "./Mumbai Indians-BattingDetails.RData"
kohli <-  getBatsmanDetails(team="Royal Challengers Bangalore",name="V Kohli",dir=".")
## [1] "./Royal Challengers Bangalore-BattingDetails.RData"

3. Runs versus deliveries (in IPL matches)

Sehwag has a superb strike rate. It can be seen that Sehwag averages around 80 runs for around 40 deliveries followed by Rohit Sharma. Raina and Dhoni average around 60 runs

p1 <-batsmanRunsVsDeliveries(raina, "SK Raina")
p2 <-batsmanRunsVsDeliveries(dhoni,"MS Dhoni")
p3 <-batsmanRunsVsDeliveries(sehwag,"V Sehwag")
p4 <-batsmanRunsVsDeliveries(gambhir,"G Gambhir")
p5 <-batsmanRunsVsDeliveries(rsharma,"RG Sharma")
p6 <-batsmanRunsVsDeliveries(kohli,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

runsVsDeliveries-1

4. Batsman Total runs, Fours and Sixes (in IPL matches)

Dhoni leads in the runs made from sixes in comparison to the others

raina46 <- select(raina,batsman,ballsPlayed,fours,sixes,runs)
p1 <-batsmanFoursSixes(raina46, "SK Raina")
dhoni46 <- select(dhoni,batsman,ballsPlayed,fours,sixes,runs)
p2 <-batsmanFoursSixes(dhoni46,"MS Dhoni")
sehwag46 <- select(sehwag,batsman,ballsPlayed,fours,sixes,runs)
p3 <-batsmanFoursSixes(sehwag46,"V Sehwag")
gambhir46 <- select(gambhir,batsman,ballsPlayed,fours,sixes,runs)
p4 <-batsmanFoursSixes(gambhir46,"G Gambhir")
rsharma46 <- select(rsharma,batsman,ballsPlayed,fours,sixes,runs)
p5 <-batsmanFoursSixes(rsharma46,"RG Sharma")
kohli46 <- select(kohli,batsman,ballsPlayed,fours,sixes,runs)
p6 <-batsmanFoursSixes(kohli46,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

foursSixes-1

5. Batsman dismissals (in IPL matches)

The type of dismissal for each batsman is shown below

p1 <-batsmanDismissals(raina, "SK Raina")
p2 <-batsmanDismissals(dhoni,"MS Dhoni")
p3 <-batsmanDismissals(sehwag,"V Sehwag")
p4 <-batsmanDismissals(gambhir,"G Gambhir")
p5 <-batsmanDismissals(rsharma,"RG Sharma")
p6 <-batsmanDismissals(kohli,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

dismissal-1

6. Runs versus Strike Rate (in IPL matches)

Raina, Dhoni and Kohli have an increasing strike rate with more runs scored

p1 <-batsmanRunsVsStrikeRate(raina, "SK Raina")
p2 <-batsmanRunsVsStrikeRate(dhoni,"MS Dhoni")
p3 <-batsmanRunsVsStrikeRate(sehwag,"V Sehwag")
p4 <-batsmanRunsVsStrikeRate(gambhir,"G Gambhir")
p5 <-batsmanRunsVsStrikeRate(rsharma,"RG Sharma")
p6 <-batsmanRunsVsStrikeRate(kohli,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

runsSR-1

7. Batsman moving average (in IPL matches)

Rohit Sharma seems to maintain an average of almost 30 runs, while Dhoni and Kohli average around 25.

p1 <-batsmanMovingAverage(raina, "SK Raina")
p2 <-batsmanMovingAverage(dhoni,"MS Dhoni")
p3 <-batsmanMovingAverage(sehwag,"V Sehwag")
p4 <-batsmanMovingAverage(gambhir,"G Gambhir")
p5 <-batsmanMovingAverage(rsharma,"RG Sharma")
p6 <-batsmanMovingAverage(kohli,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

ma-1

8. Batsman cumulative average (in IPL matches)

The cumulative runs average of Raina, Gambhir, Kohli and Rohit Sharma are around 28-30 runs.Dhoni drops to 25

p1 <-batsmanCumulativeAverageRuns(raina, "SK Raina")
p2 <-batsmanCumulativeAverageRuns(dhoni,"MS Dhoni")
p3 <-batsmanCumulativeAverageRuns(sehwag,"V Sehwag")
p4 <-batsmanCumulativeAverageRuns(gambhir,"G Gambhir")
p5 <-batsmanCumulativeAverageRuns(rsharma,"RG Sharma")
p6 <-batsmanCumulativeAverageRuns(kohli,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cAvg-1

9. Cumulative Average Strike Rate (in IPL matches)

As seen above Sehwag has a phenomenal cumulative strike rate of around 150, followed by Dhoni around 130, then we Raina and finally Kohli.

p1 <-batsmanCumulativeStrikeRate(raina, "SK Raina")
p2 <-batsmanCumulativeStrikeRate(dhoni,"MS Dhoni")
p3 <-batsmanCumulativeStrikeRate(sehwag,"V Sehwag")
p4 <-batsmanCumulativeStrikeRate(gambhir,"G Gambhir")
p5 <-batsmanCumulativeStrikeRate(rsharma,"RG Sharma")
p6 <-batsmanCumulativeStrikeRate(kohli,"V Kohli")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cSR-1

10. Batsman runs against opposition (in IPL matches)

The following charts show the performance of te batsmen against opposing IPL teams

batsmanRunsAgainstOpposition(raina, "SK Raina")

runsOppn1-1

batsmanRunsAgainstOpposition(dhoni,"MS Dhoni")

runsOppn2-1

batsmanRunsAgainstOpposition(sehwag,"V Sehwag")

runsOppn3-1

batsmanRunsAgainstOpposition(gambhir,"G Gambhir")

runsOppn4-1

batsmanRunsAgainstOpposition(rsharma,"RG Sharma")

runsOppn5-1

batsmanRunsAgainstOpposition(kohli,"V Kohli")

runsOppn6-1

11. Runs at different venues (in IPL matches)

The plots below give the performances of the batsmen at different grounds.

batsmanRunsVenue(raina, "SK Raina")

runsVenue1-1

batsmanRunsVenue(dhoni,"MS Dhoni")

runsVenue2-1

batsmanRunsVenue(sehwag,"V Sehwag")

runsVenue3-1

batsmanRunsVenue(gambhir,"G Gambhir")

runsVenue4-1

batsmanRunsVenue(rsharma,"RG Sharma")

runsVenue5-1

batsmanRunsVenue(kohli,"V Kohli")

runsVenue6-1

12. Predict number of runs to deliveries (in IPL matches)

The plots below use rpart classification tree to predict the number of deliveries required to score the runs in the leaf node.

par(mfrow=c(1,3))
par(mar=c(4,4,2,2))
batsmanRunsPredict(raina, "SK Raina")
batsmanRunsPredict(dhoni,"MS Dhoni")
batsmanRunsPredict(sehwag,"V Sehwag")

runsPredict1,runsVenue1-1

par(mfrow=c(1,3))
par(mar=c(4,4,2,2))
batsmanRunsPredict(gambhir,"G Gambhir")
batsmanRunsPredict(rsharma,"RG Sharma")
batsmanRunsPredict(kohli,"V Kohli")

runsPredict2,runsVenue1-1

B. Bowler functions

13. Get bowling details in IPL matches

The function below gets the overall team IPL T20 bowling details based on the RData file available in IPL T20 matches. This is currently also available in Github at [yorkrData] (https://github.com/tvganesh/yorkrData/tree/master/IPL/IPL-T20-matches). The IPL T20 bowling details of the IPL team in each match is created, and a huge data frame is created by rbinding the individual dataframes. This can be saved as a RData file

setwd("C:/software/cricket-package/york-test/yorkrData/IPL/IPL-T20-matches")
kkr_bowling <- getTeamBowlingDetails("Kolkata Knight Riders",dir=".",save=TRUE)
csk_bowling <- getTeamBowlingDetails("Chennai Super Kings",dir=".",save=TRUE)
kxip_bowling <- getTeamBowlingDetails("Kings XI Punjab",dir=".",save=TRUE)
mi_bowling <- getTeamBowlingDetails("Mumbai Indians",dir=".",save=TRUE)
rcb_bowling <- getTeamBowlingDetails("Royal Challengers Bangalore",dir=".",save=TRUE)
rr_bowling <- getTeamBowlingDetails("Rajasthan Royals",dir=".",save=TRUE)
fl <- list.files(".","BowlingDetails.RData")
file.copy(fl, "C:/software/cricket-package/cricsheet/cleanup/IPL/part4")

14. Get bowling details of the individual IPL bowlers

This function is used to get the individual bowling record for a specified bowler of the country as in the functions below. For analyzing the bowling performances the following cricketers have been chosen based on the bowling scorecard from my post yorkr crashes the IPL party ! – Part 3

  1. Ravichander Ashwin (CSK)
  2. DJ Bravo (CSK)
  3. PP Chawla (KXIP)
  4. Harbhajan Singh (MI)
  5. R Vinay Kumar (RCB)
  6. SK Trivedi (RR)
setwd("C:/software/cricket-package/cricsheet/cleanup/IPL/part4")
ashwin <- getBowlerWicketDetails(team="Chennai Super Kings",name="R Ashwin",dir=".")
bravo <-  getBowlerWicketDetails(team="Chennai Super Kings",name="DJ Bravo",dir=".")
chawla <-  getBowlerWicketDetails(team="Kings XI Punjab",name="PP Chawla",dir=".")
harbhajan <-  getBowlerWicketDetails(team="Mumbai Indians",name="Harbhajan Singh",dir=".")
vinay <-  getBowlerWicketDetails(team="Royal Challengers Bangalore",name="R Vinay Kumar",dir=".")
sktrivedi <-  getBowlerWicketDetails(team="Rajasthan Royals",name="SK Trivedi",dir=".")

15. Bowler Mean Economy Rate (in IPL matches)

Ashwin & Chawla have the best economy rates of in the IPL teams, followed by Harbhajan Singh

p1<-bowlerMeanEconomyRate(ashwin,"R Ashwin")
p2<-bowlerMeanEconomyRate(bravo, "DJ Bravo")
p3<-bowlerMeanEconomyRate(chawla, "PP Chawla")
p4<-bowlerMeanEconomyRate(harbhajan, "Harbhajan Singh")
p5<-bowlerMeanEconomyRate(vinay, "R Vinay")
p6<-bowlerMeanEconomyRate(sktrivedi, "SK Trivedi")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

meanER-1

16. Bowler Mean Runs conceded (in IPL matches)

p1<-bowlerMeanRunsConceded(ashwin,"R Ashwin")
p2<-bowlerMeanRunsConceded(bravo, "DJ Bravo")
p3<-bowlerMeanRunsConceded(chawla, "PP Chawla")
p4<-bowlerMeanRunsConceded(harbhajan, "Harbhajan Singh")
p5<-bowlerMeanRunsConceded(vinay, "R Vinay")
p6<-bowlerMeanRunsConceded(sktrivedi, "SK Trivedi")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

meanRunsConceded-1

17. Bowler Moving average (in IPL matches)

Harbhajan’s moving average is the best hovering around 2 wickets

p1<-bowlerMovingAverage(ashwin,"R Ashwin")
p2<-bowlerMovingAverage(bravo, "DJ Bravo")
p3<-bowlerMovingAverage(chawla, "PP Chawla")
p4<-bowlerMovingAverage(harbhajan, "Harbhajan Singh")
p5<-bowlerMovingAverage(vinay, "R Vinay")
p6<-bowlerMovingAverage(sktrivedi, "SK Trivedi")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

bowlerMA-1

17. Bowler cumulative average wickets (in IPL matches)

The cumulative average tells a different story. DJ Bravo and R Vinay have a cumulative average of 2 wickets. All others are around 1.5

p1<-bowlerCumulativeAvgWickets(ashwin,"R Ashwin")
p2<-bowlerCumulativeAvgWickets(bravo, "DJ Bravo")
p3<-bowlerCumulativeAvgWickets(chawla, "PP Chawla")
p4<-bowlerCumulativeAvgWickets(harbhajan, "Harbhajan Singh")
p5<-bowlerCumulativeAvgWickets(vinay, "R Vinay")
p6<-bowlerCumulativeAvgWickets(sktrivedi, "SK Trivedi")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cumWkts-1

18. Bowler cumulative Economy Rate (ER) (in IPL matches)

Ashwin & Harbhajan have the best cumulative economy rate

p1<-bowlerCumulativeAvgEconRate(ashwin,"R Ashwin")
p2<-bowlerCumulativeAvgEconRate(bravo, "DJ Bravo")
p3<-bowlerCumulativeAvgEconRate(chawla, "PP Chawla")
p4<-bowlerCumulativeAvgEconRate(harbhajan, "Harbhajan Singh")
p5<-bowlerCumulativeAvgEconRate(vinay, "R Vinay")
p6<-bowlerCumulativeAvgEconRate(sktrivedi, "SK Trivedi")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cumER-1

19. Bowler wicket plot (in IPL matches)

The plot below gives the average wickets versus number of overs

p1<-bowlerWicketPlot(ashwin,"R Ashwin")
p2<-bowlerWicketPlot(bravo, "DJ Bravo")
p3<-bowlerWicketPlot(chawla, "PP Chawla")
p4<-bowlerWicketPlot(harbhajan, "Harbhajan Singh")
p5<-bowlerWicketPlot(vinay, "R Vinay")
p6<-bowlerWicketPlot(sktrivedi, "SK Trivedi")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

wktPlot-1

20. Bowler wicket against opposing IPL teams

bowlerWicketsAgainstOpposition(ashwin,"R Ashwin")

wktsOppn1-1

bowlerWicketsAgainstOpposition(bravo, "DJ Bravo")

wktsOppn2-1

bowlerWicketsAgainstOpposition(chawla, "PP Chawla")

wktsOppn3-1

bowlerWicketsAgainstOpposition(harbhajan, "Harbhajan Singh")

wktsOppn4-1

bowlerWicketsAgainstOpposition(vinay, "R Vinay")

wktsOppn5-1

bowlerWicketsAgainstOpposition(sktrivedi, "SK Trivedi")

wktsOppn6-1

21. Bowler wicket at cricket grounds in IPL

bowlerWicketsVenue(ashwin,"R Ashwin")

wktsAve1-1

bowlerWicketsVenue(bravo, "DJ Bravo")

wktsAve2-1

bowlerWicketsVenue(chawla, "PP Chawla")

wktsAve3-1

bowlerWicketsVenue(harbhajan, "Harbhajan Singh")

wktsAve4-1

bowlerWicketsVenue(vinay, "R Vinay")

wktsAve5-1

bowlerWicketsVenue(sktrivedi, "SK Trivedi")

wktsAve6-1

22. Get Delivery wickets for IPL bowlers

This function creates a dataframe of deliveries and the wickets taken

setwd("C:/software/cricket-package/york-test/yorkrData/IPL/IPL-T20-matches")
ashwin1 <- getDeliveryWickets(team="Chennai Super Kings",dir=".",name="R Ashwin",save=FALSE)
bravo1 <- getDeliveryWickets(team="Chennai Super Kings",dir=".",name="DJ Bravo",save=FALSE)
chawla1 <- getDeliveryWickets(team="Kings XI Punjab",dir=".",name="PP Chawla",save=FALSE)
harbhajan1 <- getDeliveryWickets(team="Mumbai Indians",dir=".",name="Harbhajan Singh",save=FALSE)
vinay1 <- getDeliveryWickets(team="Royal Challengers Bangalore",dir=".",name="R Vinay",save=FALSE)
sktrivedi1 <- getDeliveryWickets(team="Rajasthan Royals",dir=".",name="SK Trivedi",save=FALSE)

23. Predict number of deliveries to wickets in IPL T20

#Ashwin takes <6.5 deliveries for a wicket while Bravo takes around 5.5 deliveries. Don't get hung up on the .5 delivery. We can just take it that Bravo will provide a breakthrough quicker than Ashwin
par(mfrow=c(1,2))
par(mar=c(4,4,2,2))

bowlerWktsPredict(ashwin1,"R Ashwin")
bowlerWktsPredict(bravo1, "DJ Bravo")

wktsPred1-1

par(mfrow=c(1,2))
par(mar=c(4,4,2,2))
bowlerWktsPredict(chawla1, "PP Chawla")
bowlerWktsPredict(harbhajan1, "Harbhajan Singh")

wktsPred2-1

par(mfrow=c(1,2))
par(mar=c(4,4,2,2))
bowlerWktsPredict(vinay1, "R Vinay")
bowlerWktsPredict(sktrivedi1, "SK Trivedi")

wktsPred3-1

Conclusion

This concludes the 4 part writeup of yorkr’s handling of IPL Twenty20’s. You can fork/clone the code from Github at yorkr.

As I mentioned earlier, this brings to a close to all my posts based on my R cricket package yorkr. I do have a couple of more ideas, but this will take some time I think.

Hope you have a great time with my yorkr package!

Till next time, adieu!

Also see

  1. Introducing cricket package yorkr: Part 3-Foxed by flight!
  2. Introducing cricketr! : An R package to analyze performances of cricketers
  3. Cricket analytics with cricketr in paperback and Kindle versions
  4. Bend it like Bluemix, MongoDB with auto-scaling – Part 1
  5. The dark side of the Internet
  6. Modeling a Car in Android
  7. yorkr pads up for the Twenty20s: Part 1- Analyzing team”s match performance
  8. Cricket analytics with cricketr

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)