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
- yorkr crashes the IPL party ! – Part 1
- yorkr crashes the IPL party ! – Part 2
- 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
- batsmanRunsVsDeliveries
- batsmanFoursSixes
- batsmanDismissals
- batsmanRunsVsStrikeRate
- batsmanMovingAverage
- batsmanCumulativeAverageRuns
- batsmanCumulativeStrikeRate
- batsmanRunsAgainstOpposition
- batsmanRunsVenue
- batsmanRunsPredict
Bowler functions
- bowlerMeanEconomyRate
- bowlerMeanRunsConceded
- bowlerMovingAverage
- bowlerCumulativeAvgWickets
- bowlerCumulativeAvgEconRate
- bowlerWicketPlot
- bowlerWicketsAgainstOpposition
- bowlerWicketsVenue
- 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
- Suresh Raina (CSK)
- MS Dhoni (CSK)
- Virendar Sehwag (DD)
- Rohit Sharma (MI)
- Gautham Gambhir (KKR)
- 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)
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)
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)
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)
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)
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)
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)
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")
batsmanRunsAgainstOpposition(dhoni,"MS Dhoni")
batsmanRunsAgainstOpposition(sehwag,"V Sehwag")
batsmanRunsAgainstOpposition(gambhir,"G Gambhir")
batsmanRunsAgainstOpposition(rsharma,"RG Sharma")
batsmanRunsAgainstOpposition(kohli,"V Kohli")
11. Runs at different venues (in IPL matches)
The plots below give the performances of the batsmen at different grounds.
batsmanRunsVenue(raina, "SK Raina")
batsmanRunsVenue(dhoni,"MS Dhoni")
batsmanRunsVenue(sehwag,"V Sehwag")
batsmanRunsVenue(gambhir,"G Gambhir")
batsmanRunsVenue(rsharma,"RG Sharma")
batsmanRunsVenue(kohli,"V Kohli")
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")
par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) batsmanRunsPredict(gambhir,"G Gambhir") batsmanRunsPredict(rsharma,"RG Sharma") batsmanRunsPredict(kohli,"V Kohli")
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
- Ravichander Ashwin (CSK)
- DJ Bravo (CSK)
- PP Chawla (KXIP)
- Harbhajan Singh (MI)
- R Vinay Kumar (RCB)
- 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)
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)
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)
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)
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)
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)
20. Bowler wicket against opposing IPL teams
bowlerWicketsAgainstOpposition(ashwin,"R Ashwin")
bowlerWicketsAgainstOpposition(bravo, "DJ Bravo")
bowlerWicketsAgainstOpposition(chawla, "PP Chawla")
bowlerWicketsAgainstOpposition(harbhajan, "Harbhajan Singh")
bowlerWicketsAgainstOpposition(vinay, "R Vinay")
bowlerWicketsAgainstOpposition(sktrivedi, "SK Trivedi")
21. Bowler wicket at cricket grounds in IPL
bowlerWicketsVenue(ashwin,"R Ashwin")
bowlerWicketsVenue(bravo, "DJ Bravo")
bowlerWicketsVenue(chawla, "PP Chawla")
bowlerWicketsVenue(harbhajan, "Harbhajan Singh")
bowlerWicketsVenue(vinay, "R Vinay")
bowlerWicketsVenue(sktrivedi, "SK Trivedi")
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")
par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) bowlerWktsPredict(chawla1, "PP Chawla") bowlerWktsPredict(harbhajan1, "Harbhajan Singh")
par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) bowlerWktsPredict(vinay1, "R Vinay") bowlerWktsPredict(sktrivedi1, "SK Trivedi")
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
- Introducing cricket package yorkr: Part 3-Foxed by flight!
- Introducing cricketr! : An R package to analyze performances of cricketers
- Cricket analytics with cricketr in paperback and Kindle versions
- Bend it like Bluemix, MongoDB with auto-scaling – Part 1
- The dark side of the Internet
- Modeling a Car in Android
- yorkr pads up for the Twenty20s: Part 1- Analyzing team”s match performance
- Cricket analytics with cricketr
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.