Inswinger: yorkr swings into International T20s


In this post I introduce ‘Inswinger’ an interactive Shiny app to analyze International T20 players, matches and teams. This app was a natural consequence to my earlier Shiny app ‘GooglyPlus’. Most of the structure for this app remained the same, I only had to work with a different dataset, so to speak.

The Googly Shiny app is based on my R package ‘yorkr’ which is now available in CRAN. The R package and hence this Shiny app is based on data from Cricsheet. Inswinger is based on the latest data dump from Cricsheet (Dec 2016) and includes all International T20 till then. There are a lot of new Internationation teams like Oman, Hong Kong, UAE, etc. In total there are 22 different International T20 teams in my Inswinger app.

The countries are a) Afghanistan b) Australia c) Bangladesh d) Bermuda e) Canada f) England g) Hong Kong h) India i) Ireland j) Kenya k) Nepal l) Netherlands m) New Zealand n) Oman o) Pakistan p) Papua New Guinea q) Scotland r) South Africa s) Sri Lanka t) United Arab Emirates u) West Indies v) Zimbabwe

My R package ‘yorkr’,  on which both these Shiny apps are based, has the ability to output either a dataframe or plot, depending on a parameter plot=TRUE or FALSE. Hence in the Inswinger Shiny app results can be displayed both as table or a plot depending on the choice of function.

Inswinger can do detailed analyses of a) Individual T20 batsman b) Individual T20 bowler c) Any T20 match d) Head to head confrontation between 2 T20 teams e) All matches of a T20 team against all other teams.

The Shiny app can be accessed at Inswinger

The code for Inswinger is available at Github. Feel free to clone/download/fork  the code from Inswinger

Based on the 5 detailed analysis domains there are 5 tabs
A) T20 Batsman: This tab can be used to perform analysis of all T20 batsman. If a batsman has played in more than 1 team, then the overall performance is considered. There are 10 functions for the T20 Batsman. They are shown below
– Batsman Runs vs. Deliveries
– Batsman’s Fours & Sixes
– Dismissals of batsman
– Batsman’s Runs vs Strike Rate
– Batsman’s Moving Average
– Batsman’s Cumulative Average Run
– Batsman’s Cumulative Strike Rate
– Batsman’s Runs against Opposition
– Batsman’s Runs at Venue
– Predict Runs of batsman

B) T20 Bowler: This tab can be used to analyze individual T20 bowlers. The functions handle T20 bowlers who have played in more than 1 T20 team.
– Mean Economy Rate of bowler
– Mean runs conceded by bowler
– Bowler’s Moving Average
– Bowler’s Cumulative Avg. Wickets
– Bowler’s Cumulative Avg. Economy Rate
– Bowler’s Wicket Plot
– Bowler’s Wickets against opposition
– Bowler’s Wickets at Venues
– Bowler’s wickets prediction

C) T20 match: This tab can be used for analyzing individual T20 matches. The available functions are
– Match Batting Scorecard – Table
– Batting Partnerships – Plot, Table
– Batsmen vs Bowlers – Plot, Table
– Match Bowling Scorecard   – Table
– Bowling Wicket Kind – Plot, Table
– Bowling Wicket Runs – Plot, Table
– Bowling Wicket Match – Plot, Table
– Bowler vs Batsmen – Plot, Table
– Match Worm Graph – Plot

D) Head to head: This tab can be used for analyzing head-to-head confrontations, between any 2 T20 teams for e.g. all matches between India vs Australia or West Indies vs Sri Lanka . The available functions are
-Team Batsmen Batting Partnerships All Matches – Plot, Table {Summary and Detailed}
-Team Batting Scorecard All Matches – Table
-Team Batsmen vs Bowlers all Matches – Plot, Table
-Team Wickets Opposition All Matches – Plot, Table
-Team Bowling Scorecard All Matches – Table
-Team Bowler vs Batsmen All Matches – Plot, Table
-Team Bowlers Wicket Kind All Matches – Plot, Table
-Team Bowler Wicket Runs All Matches – Plot, Table
– Win Loss All Matches – Plot

E) T20 team’s overall performance: this tab can be used analyze the overall performance of any T20 team. For this analysis all matches played by this team is considered. The available functions are
-Team Batsmen Partnerships Overall – Plot, Table {Summary and Detailed)}
-Team Batting Scorecard Overall –Table
-Team Batsmen vs Bowlers Overall – Plot, Table
-Team Bowler vs Batsmen Overall – Plot, Table
-Team Bowling Scorecard Overall – Table
-Team Bowler Wicket Kind Overall – Plot, Table

Below I include a random set of charts that are generated in each of the 5 tabs
A. IPL Batsman
a. Shakib-al-Hassan (Bangladesh) :  Runs vs Deliveries
untitled

b. Virat Kohli (India) – Cumulative Average
untitled

c.  AB Devilliers (South Africa) – Runs at venues
untitled

d. Glenn Maxwell (Australia)  – Predict runs vs deliveries faces
untitled

B. IPL Bowler
a. TG Southee (New Zealand) – Mean Economy Rate vs overs
untitled

b) DJ Bravo – Moving Average of wickets
untitled

c) AC Evans (Scotland) – Bowler Wickets Against Opposition
untitled

C.T20 Match
a. Match Score (Afghanistan vs Canada, 2012-03-18)
untitled

b)  Match batting partnerships (Plot) Hong Kong vs Oman (2015-11-21), Hong Kong
Hong Kong Partnerships
untitled

c) Match batting partnerships (Table) – Ireland vs Scotland(2012-03-18, Ireland)
Batting partnership can also be displayed as a table
untitled

d) Batsmen vs Bowlers (Plot) – India vs England (2012-12-22)
untitled

e) Match Worm Chart – Sri Lanka vs Pakistan (2015-08-01)
untitled

D.Head to head
a) Team Batsmen Partnership (Plot) – India vs Australia (all matches)
Virat Kohli has the highest total runs in partnerships against Australia
untitled

b)  Team Batsmen Partnership (Summary – Table) – Kenya vs Bangladesh
untitled

c) Team Bowling Scorecard (Table only) India vs South Africa all Matches
untitled

d) Wins- Losses New Zealand vs West Indies all Matches
untitled

C) Overall performances
a) Batting Scorecard All Matches  (Table only) – England’s overall batting performance
Eoin Morgan, Kevin Pieterson  & SJ Taylor have the best performance
untitled

b) Batsman vs Bowlers all Matches (Plot)
India’s best performing batsman (Rank=1) is Virat Kohli
untitled

c)  Batsman vs Bowlers all Matches (Table)
The plot above for Virat Kohli can also be displayed as a table. Kohli has score most runs DJ Bravo, SR Watson & Shahid Afridi
untitled

The Inswinger Shiny app can be accessed at Inswinger. Give it a swing!

The code for Inswinger is available at Github. Feel free to clone/download/fork  the code from Inswinger

Also see my other Shiny apps
1.GooglyPlus
2.What would Shakespeare say?
3.Sixer
4.Revisiting crimes against women in India

You may also like
1. Neural Networks: The mechanics of backpropagation
A primer on Qubits, Quantum gates and Quantum Operation
2. Re-working the Lucy Richardson algorithm in OpenCV
3.Design Principles of Scalable, Distributed Systems
4.Spicing up a IBM Bluemix cloud app with MongoDB and NodeExpress
5.Programming languages in layman’s language
7.Re-introducing cricketr! : An R package to analyze performances of cricketers

To see all posts take at a look at Index of Posts

Neural Networks: On Perceptrons and Sigmoid Neurons


Neural Networks had their beginnings in 1943 when Warren McCulloch, a neurophysiologist, and a young mathematician, Walter Pitts, wrote a paper on how neurons might work.  Much later in 1958, Frank Rosenblatt, a neuro-biologist proposed the Perceptron. The Perceptron is a computer model or computerized machine which is devised to represent or simulate the ability of the brain to recognize and discriminate. In machine learning, the perceptron is an algorithm for supervised learning of binary classifiers

Initially it was believed that  Perceptrons were capable of many things including “the ability to walk, talk, see, write, reproduce itself and be conscious of its existence.”

However, a subsequent paper by Marvin Minky and Seymour Papert of MIT, titled “Perceptrons” proved that the Perceptron was truly limited in its functionality. Specifically they showed that the Perceptron was incapable of producing XOR functionality. The Perceptron is only capable of classification where the data points are linearly separable.

This post implements the simple learning algorithm of the ‘Linear Perceptron’ and the ‘Sigmoid Perceptron’.  The implementation has been done in Octave. This implementation is based on “Neural networks for Machine Learning” course by Prof Geoffrey Hinton at Coursera

Perceptron learning procedure
z = ∑wixi  + b
where wi is the ith weight and xi is the ith  feature

For every training case compute the activation output zi

  • If the output classifies correctly, leave the weights alone
  • If the output classifies a ‘0’ as a ‘1’, then subtract the the feature from the weight
  • If the output classifies a ‘0’ as a ‘1’, then add the feature to the weight

This simple neural network is represented below
perceptron

Sigmoid neuron learning procedure
zi = sigmoid(∑wixi  + b)
where sigmoid is
sigmoid(z) = 1/1+e^{-z}

Hence
z_{i} = 1/1+e^{-(\sum w_{i}x_{i}+b)}
For every training case compute the activation output zi

  • If the output classifies correctly, leave the weights alone
  • If the output incorrectly classifies a ‘0’ as a ‘1’ i.e. z_{i} >sigmoid(0), then subtract the feature from the weight
  • If the output incorrectly classifies a ‘1’ as ‘0’ i.e., i.e z_{i} < sigmoid(0), then add the feature to the weight
  • Iterate till errors <= 1

This is shown below
sigmoid_neuron

I have implemented the learning algorithm of the Perceptron and Sigmoid Neuron in Octave. The code is available at Github at Perceptron.

  1. Perceptron execution

I performed the tests on 2 different datasets

Data 1
untitled

Data 2
untitled

2. Sigmoid Perceptron execution
Data 1 & Data 2

It can be seen that the Perceptron does work for simple linearly separable data. I will be implementing other more advanced Neural Networks in the months to come.

Watch this space!

GooglyPlus: yorkr analyzes IPL players, teams, matches with plots and tables


In this post I introduce my new Shiny app,“GooglyPlus”, which is a  more evolved version of my earlier Shiny app “Googly”. My R package ‘yorkr’,  on which both these Shiny apps are based, has the ability to output either a dataframe or plot, depending on a parameter plot=TRUE or FALSE. My initial version of the app only included plots, and did not exercise the yorkr package fully. Moreover, I am certain, there may be a set of cricket aficionados who would prefer, numbers to charts. Hence I have created this enhanced version of the Googly app and appropriately renamed it as GooglyPlus. GooglyPlus is based on the yorkr package which uses data from Cricsheet. The app is based on IPL data from  all IPL matches from 2008 up to 2016. Feel free to clone/fork or download the code from Github at GooglyPlus.

Click  GooglyPlus to access the Shiny app!

The changes for GooglyPlus over the earlier Googly app is only in the following 3 tab panels

  • IPL match
  • Head to head
  • Overall Performance

The analysis of IPL batsman and IPL bowler tabs are unchanged. These charts are as they were before.

The changes are only in  tabs i) IPL match ii) Head to head and  iii) Overall Performance. New functionality has been added and existing functions now have the dual option of either displaying a plot or a table.

The changes are

A) IPL Match
The following additions/enhancements have been done

-Match Batting Scorecard – Table
-Batting Partnerships – Plot, Table (New)
-Batsmen vs Bowlers – Plot, Table(New)
-Match Bowling Scorecard   – Table (New)
-Bowling Wicket Kind – Plot, Table (New)
-Bowling Wicket Runs – Plot, Table (New)
-Bowling Wicket Match – Plot, Table (New)
-Bowler vs Batsmen – Plot, Table (New)
-Match Worm Graph – Plot

B) Head to head
The following functions have been added/enhanced

-Team Batsmen Batting Partnerships All Matches – Plot, Table {Summary (New) and Detailed (New)}
-Team Batting Scorecard All Matches – Table (New)
-Team Batsmen vs Bowlers all Matches – Plot, Table (New)
-Team Wickets Opposition All Matches – Plot, Table (New)
-Team Bowling Scorecard All Matches – Table (New)
-Team Bowler vs Batsmen All Matches – Plot, Table (New)
-Team Bowlers Wicket Kind All Matches – Plot, Table (New)
-Team Bowler Wicket Runs All Matches – Plot, Table (New)
-Win Loss All Matches – Plot

C) Overall Performance
The following additions/enhancements have been done in this tab

-Team Batsmen Partnerships Overall – Plot, Table {Summary (New) and Detailed (New)}
-Team Batting Scorecard Overall –Table (New)
-Team Batsmen vs Bowlers Overall – Plot, Table (New)
-Team Bowler vs Batsmen Overall – Plot, Table (New)
-Team Bowling Scorecard Overall – Table (New)
-Team Bowler Wicket Kind Overall – Plot, Table (New)

Included below are some random charts and tables. Feel free to explore the Shiny app further

1) IPL Match
a) Match Batting Scorecard (Table only)
This is the batting score card for the Chennai Super Kings & Deccan Chargers 2011-05-11

untitled

b)  Match batting partnerships (Plot)
Delhi Daredevils vs Kings XI Punjab – 2011-04-23

untitled

c) Match batting partnerships (Table)
The same batting partnership  Delhi Daredevils vs Kings XI Punjab – 2011-04-23 as a table

untitled

d) Batsmen vs Bowlers (Plot)
Kolkata Knight Riders vs Mumbai Indians 2010-04-19

Untitled.png

e)  Match Bowling Scorecard (Table only)
untitled

B) Head to head

a) Team Batsmen Partnership (Plot)
Deccan Chargers vs Kolkata Knight Riders all matches

untitled

b)  Team Batsmen Partnership (Summary – Table)
In the following tables it can be seen that MS Dhoni has performed better that SK Raina  CSK against DD matches, whereas SK Raina performs better than Dhoni in CSK vs  KKR matches

i) Chennai Super Kings vs Delhi Daredevils (Summary – Table)

untitled

ii) Chennai Super Kings vs Kolkata Knight Riders (Summary – Table)
untitled

iii) Rising Pune Supergiants vs Gujarat Lions (Detailed – Table)
This table provides the detailed partnership for RPS vs GL all matches

untitled

c) Team Bowling Scorecard (Table only)
This table gives the bowling scorecard of Pune Warriors vs Deccan Chargers in all matches

untitled

C) Overall performances
a) Batting Scorecard All Matches  (Table only)

This is the batting scorecard of Royal Challengers Bangalore. The top 3 batsmen are V Kohli, C Gayle and AB Devilliers in that order

untitled

b) Batsman vs Bowlers all Matches (Plot)
This gives the performance of Mumbai Indian’s batsman of Rank=1, which is Rohit Sharma, against bowlers of all other teams

untitled

c)  Batsman vs Bowlers all Matches (Table)
The above plot as a table. It can be seen that Rohit Sharma has scored maximum runs against M Morkel, then Shakib Al Hasan and then UT Yadav.

untitled

d) Bowling scorecard (Table only)
The table below gives the bowling scorecard of CSK. R Ashwin leads with a tally of 98 wickets followed by DJ Bravo who has 88 wickets and then JA Morkel who has 83 wickets in all matches against all teams

Untitled.png

This is just a random selection of functions. Do play around with the app and checkout how the different IPL batsmen, bowlers and teams stack against each other. Do read my earlier post Googly: An interactive app for analyzing IPL players, matches and teams using R package yorkr  for more details about the app and other functions available.

Click GooglyPlus to access the Shiny app!

You can clone/fork/download the code from Github at GooglyPlus

Hope you have fun playing around with the Shiny app!

Note: In the tabs, for some of the functions, not all controls  are required. It is possible to enable the controls selectively but this has not been done in this current version. I may make the changes some time in the future.

Take a look at my other Shiny apps
a.Revisiting crimes against women in India
b. Natural language processing: What would Shakespeare say?

Check out some of my other posts
1. Analyzing World Bank data with WDI, googleVis Motion Charts
2. Video presentation on Machine Learning, Data Science, NLP and Big Data – Part 1
3. Singularity
4. Design principles of scalable, distributed systems
5. Simulating an Edge shape in Android
6. Dabbling with Wiener filter in OpenCV

To see all posts click Index of Posts

yorkr ranks IPL Players post 2016 season


Here is a short post which ranks IPL batsmen and bowlers post the 2016 IPL season. These are based on match data from Cricsheet. I had already ranked IPL players in my post yorkr ranks IPL batsmen and bowlers, but that was mid IPL 2016 season. This post will be final ranking post 2016 season

This post has also been published in RPubs RankIPLPlayers2016. You can download this as a pdf file at RankIPLPlayers2016.pdf.

You can take a look at the code at rankIPLPlayers2016

Checkout my interactive Shiny apps GooglyPlus (plots & tables) and Googly (only plots) which can be used to analyze IPL players, teams and matches.

rm(list=ls())
library(yorkr)
library(dplyr)
source('C:/software/cricket-package/cricsheet/ipl2016/final/R/rankIPLBatsmen.R', encoding = 'UTF-8')
source('C:/software/cricket-package/cricsheet/ipl2016/final/R/rankIPLBowlers.R', encoding = 'UTF-8')

Rank IPL batsmen post 2016

Chris Gayle, Shaun Marsh & David Warner are top 3 IPL batsmen. Gayle towers over everybody, with an 38.28 Mean Runs, and a Mean Strike Rate of 138.85. Virat Kohli comes in 4th, with 34.52 as his Average Runs per innings, and a Mean Strike Rate of 117.51

iplBatsmanRank <- rankIPLBatsmen()
as.data.frame(iplBatsmanRank[1:30,])
##             batsman matches meanRuns    meanSR
## 1          CH Gayle      92 38.28261 138.85120
## 2          SE Marsh      60 36.40000 118.97783
## 3         DA Warner     104 34.51923 124.88798
## 4           V Kohli     136 31.77941 117.51000
## 5         AM Rahane      89 31.46067 104.62989
## 6    AB de Villiers     109 29.93578 136.48945
## 7      SR Tendulkar      78 29.62821 108.58962
## 8         G Gambhir     133 28.94737 109.61263
## 9         RG Sharma     140 28.68571 117.79057
## 10         SK Raina     143 28.41259 121.55713
## 11        SR Watson      90 28.21111 125.80122
## 12         S Dhawan     110 28.09091 111.97282
## 13         R Dravid      79 27.87342 109.14544
## 14         DR Smith      76 27.55263 120.22329
## 15        JP Duminy      70 27.28571 122.99243
## 16      BB McCullum      94 26.86170 118.55606
## 17        JH Kallis      97 26.83505  95.47866
## 18         V Sehwag     105 26.26667 137.11562
## 19       RV Uthappa     132 26.18182 123.16326
## 20     AC Gilchrist      81 25.77778 122.69074
## 21          M Vijay      99 25.69697 106.02010
## 22    KC Sangakkara      70 25.67143 112.97529
## 23         MS Dhoni     131 25.14504 131.62206
## 24        DA Miller      60 24.76667 133.80983
## 25        AT Rayudu      99 23.35354 121.59313
## 26 DPMD Jayawardene      80 23.05000 114.54712
## 27     Yuvraj Singh     103 22.46602 118.15000
## 28        DJ Hussey      63 22.26984        NA
## 29        YK Pathan     121 22.25620 132.58793
## 30      S Badrinath      66 22.22727 114.97061

Rank IPL bowlers

The top 3 IPL T20 bowlers are SL Malinga, DJ Bravo and SP Narine

Don’t get hung up on the decimals in the average wickets for the bowlers. All it implies is that if 2 bowlers have average wickets of 1.0 and 1.5, it implies that in 2 matches the 1st bowler will take 2 wickets and the 2nd bowler will take 3 wickets.

setwd("C:/software/cricket-package/cricsheet/ipl2016/details")
iplBowlersRank <- rankIPLBowlers()
as.data.frame(iplBowlersRank[1:30,])
##             bowler matches meanWickets   meanER
## 1       SL Malinga      96    1.645833 6.545208
## 2         DJ Bravo      58    1.517241 7.929310
## 3        SP Narine      65    1.492308 6.155077
## 4          B Kumar      45    1.422222 7.355556
## 5        YS Chahal      41    1.414634 8.057073
## 6         M Morkel      37    1.405405 7.626216
## 7        IK Pathan      40    1.400000 7.579250
## 8         RP Singh      42    1.357143 7.966429
## 9         MM Patel      31    1.354839 7.282581
## 10   R Vinay Kumar      63    1.317460 8.342540
## 11  Sandeep Sharma      38    1.315789 7.697368
## 12       MM Sharma      46    1.304348 7.740652
## 13         P Awana      33    1.303030 8.325758
## 14        MM Patel      30    1.300000 7.569667
## 15          Z Khan      41    1.292683 7.735854
## 16         PP Ojha      53    1.245283 7.268679
## 17     JP Faulkner      40    1.225000 8.502250
## 18 Shakib Al Hasan      41    1.170732 7.103659
## 19     DS Kulkarni      32    1.156250 8.372188
## 20        UT Yadav      46    1.152174 8.394783
## 21        A Kumble      41    1.146341 6.567073
## 22       JA Morkel      73    1.136986 8.131370
## 23        SK Warne      53    1.132075 7.277170
## 24        A Mishra      55    1.127273 7.319455
## 25        UT Yadav      33    1.090909 8.853636
## 26        L Balaji      34    1.088235 7.186176
## 27       PP Chawla      35    1.085714 8.162000
## 28        R Ashwin      92    1.065217 6.812391
## 29  M Muralitharan      39    1.051282 6.470256
## 30 Harbhajan Singh     120    1.050000 7.134833

IBM Data Science Experience:  First steps with yorkr


Fresh, and slightly dizzy, from my foray into Quantum Computing with IBM’s Quantum Experience, I now turn my attention to IBM’s Data Science Experience (DSE).

I am on the verge of completing a really great 3 module ‘Data Science and Engineering with Spark XSeries’ from the University of California, Berkeley and I have been thinking of trying out some form of integrated delivery platform for performing analytics, for quite some time.  Coincidentally,  IBM comes out with its Data Science Experience. a month back. There are a couple of other collaborative platforms available for playing around with Apache Spark or Data Analytics namely Jupyter notebooks, Databricks, Data.world.

I decided to go ahead with IBM’s Data Science Experience as  the GUI is a lot cooler, includes shared data sets and integrates with Object Storage, Cloudant DB etc,  which seemed a lot closer to the cloud, literally!  IBM’s DSE is an interactive, collaborative, cloud-based environment for performing data analysis with Apache Spark. DSE is hosted on IBM’s PaaS environment, Bluemix. It should be possible to access in DSE the plethora of cloud services available on Bluemix. IBM’s DSE uses Jupyter notebooks for creating and analyzing data which can be easily shared and has access to a few hundred publicly available datasets

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

In this post, I use IBM’s DSE and my R package yorkr, for analyzing the performance of 1 ODI match (Aus-Ind, 2 Feb 2012)  and the batting performance of Virat Kohli in IPL matches. These are my ‘first’ steps in DSE so, I use plain old “R language” for analysis together with my R package ‘yorkr’. I intend to  do more interesting stuff on Machine learning with SparkR, Sparklyr and PySpark in the weeks and months to come.

You can checkout the Jupyter notebooks created with IBM’s DSE Y at Github  – “Using R package yorkr – A quick overview’ and  on NBviewer at “Using R package yorkr – A quick overview

Working with Jupyter notebooks are fairly straight forward which can handle code in R, Python and Scala. Each cell can either contain code (Python or Scala), Markdown text, NBConvert or Heading. The code is written into the cells and can be executed sequentially. Here is a screen shot of the notebook.

Untitled

The ‘File’ menu can be used for ‘saving and checkpointing’ or ‘reverting’ to a checkpoint. The ‘kernel’ menu can be used to start, interrupt, restart and run all cells etc. Data Sources icon can be used to load data sources to your code. The data is uploaded to Object Storage with appropriate credentials. You will have to  import this data from Object Storage using the credentials. In my notebook with yorkr I directly load the data from Github.  You can use the sharing to share the notebook. The shared notebook has an extension ‘ipynb’. You can use the ‘Sharing’ icon  to share the notebook. The shared notebook has an extension ‘ipynb’. You an import this notebook directly into your environment and can get started with the code available in the notebook.

You can import existing R, Python or Scala notebooks as shown below. My notebook ‘Using R package yorkr – A quick overview’ can be downloaded using the link ‘yorkrWithDSE’ and clicking the green download icon on top right corner.

Untitled2

I have also uploaded the file to Github and you can download from here too ‘yorkrWithDSE’. This notebook can be imported into your DSE as shown below

Untitled1

Jupyter notebooks have been integrated with Github and are rendered directly from Github.  You can view my Jupyter notebook here  – “Using R package yorkr – A quick overview’. You can also view it on NBviewer at “Using R package yorkr – A quick overview

So there it is. You can download my notebook, import it into IBM’s Data Science Experience and then use data from ‘yorkrData” as shown. As already mentioned yorkrData contains converted data for ODIs, T20 and IPL. For details on how to use my R package yorkr  please my posts on yorkr at “Index of posts

Hope you have fun playing wit IBM’s Data Science Experience and my package yorkr.

I will be exploring IBM’s DSE in weeks and months to come in the areas of Machine Learning with SparkR,SparklyR or pySpark.

Watch this space!!!

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

Also see

1. Introducing QCSimulator: A 5-qubit quantum computing simulator in R
2. Natural Processing Language : What would Shakespeare say?
3. Introducing cricket package yorkr:Part 1- Beaten by sheer pace!
4. A closer look at “Robot horse on a Trot! in Android”
5.  Re-introducing cricketr! : An R package to analyze performances of cricketers
6.   What’s up Watson? Using IBM Watson’s QAAPI with Bluemix, NodeExpress – Part 1
7.  Deblurring with OpenCV: Wiener filter reloaded

To see all my posts check
Index of posts

Introducing QCSimulator: A 5-qubit quantum computing simulator in R


Introduction

My 5-qubit Quantum Computing Simulator,QCSimulator, is finally ready, and here it is! I have been able to successfully complete this simulator by working through a fair amount of material. To a large extent, the simulator is easy, if one understands how to solve the quantum circuit. However the theory behind quantum computing itself, is quite formidable, and I hope to scale this mountain over a period of time.

QCSimulator is now on CRAN!!!

The code for the QCSimulator package is also available at Github QCSimulator. This post has also been published at Rpubs as QCSimulator and can be downloaded as a PDF document at QCSimulator.pdf

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

install.packages("QCSimulator")
library(QCSimulator)
library(ggplot2)

1. Initialize the environment and set global variables

Here I initialize the environment with global variables and then display a few of them.

rm(list=ls())
#Call the init function to initialize the environment and create global variables
init()

# Display some of global variables in environment
ls()
##  [1] "I16"     "I2"      "I4"      "I8"      "q0_"     "q00_"    "q000_"  
##  [8] "q0000_"  "q00000_" "q00001_" "q0001_"  "q00010_" "q00011_" "q001_"  
## [15] "q0010_"  "q00100_" "q00101_" "q0011_"  "q00110_" "q00111_" "q01_"   
## [22] "q010_"   "q0100_"  "q01000_" "q01001_" "q0101_"  "q01010_" "q01011_"
## [29] "q011_"   "q0110_"  "q01100_" "q01101_" "q0111_"  "q01111_" "q1_"    
## [36] "q10_"    "q100_"   "q1000_"  "q10000_" "q10001_" "q1001_"  "q10010_"
## [43] "q10011_" "q101_"   "q1010_"  "q10100_" "q10101_" "q1011_"  "q10110_"
## [50] "q10111_" "q11_"    "q110_"   "q1100_"  "q11000_" "q11001_" "q1101_" 
## [57] "q11010_" "q11011_" "q111_"   "q1110_"  "q11100_" "q11101_" "q1111_" 
## [64] "q11110_" "q11111_"
#1. 2 x 2 Identity matrix 
I2
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
#2. 8 x 8 Identity matrix 
I8
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    0    0    0    0    0    0    0
## [2,]    0    1    0    0    0    0    0    0
## [3,]    0    0    1    0    0    0    0    0
## [4,]    0    0    0    1    0    0    0    0
## [5,]    0    0    0    0    1    0    0    0
## [6,]    0    0    0    0    0    1    0    0
## [7,]    0    0    0    0    0    0    1    0
## [8,]    0    0    0    0    0    0    0    1
#3. Qubit |00>
q00_
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## [4,]    0
#4. Qubit |010>
q010_
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    1
## [4,]    0
## [5,]    0
## [6,]    0
## [7,]    0
## [8,]    0
#5. Qubit |0100>
q0100_
##       [,1]
##  [1,]    0
##  [2,]    0
##  [3,]    0
##  [4,]    0
##  [5,]    1
##  [6,]    0
##  [7,]    0
##  [8,]    0
##  [9,]    0
## [10,]    0
## [11,]    0
## [12,]    0
## [13,]    0
## [14,]    0
## [15,]    0
## [16,]    0
#6. Qubit 10010
q10010_
##       [,1]
##  [1,]    0
##  [2,]    0
##  [3,]    0
##  [4,]    0
##  [5,]    0
##  [6,]    0
##  [7,]    0
##  [8,]    0
##  [9,]    0
## [10,]    0
## [11,]    0
## [12,]    0
## [13,]    0
## [14,]    0
## [15,]    0
## [16,]    0
## [17,]    0
## [18,]    0
## [19,]    1
## [20,]    0
## [21,]    0
## [22,]    0
## [23,]    0
## [24,]    0
## [25,]    0
## [26,]    0
## [27,]    0
## [28,]    0
## [29,]    0
## [30,]    0
## [31,]    0
## [32,]    0

The QCSimulator implements the following gates

  1. Pauli X,Y,Z, S,S’, T, T’ gates
  2. Rotation , Hadamard,CSWAP,Toffoli gates
  3. 2,3,4,5 qubit CNOT gates e.g CNOT2_01,CNOT3_20,CNOT4_13 etc
  4. Toffoli State,SWAPQ0Q1

2. To display the unitary matrix of gates

To check the unitary matrix of gates, we need to pass the appropriate identity matrix as an argument. Hence below the qubit gates require a 2 x 2 unitary matrix and the 2 & 3 qubit CNOT gates require a 4 x 4 and 8 x 8 identity matrix respectively

PauliX(I2)
##      [,1] [,2]
## [1,]    0    1
## [2,]    1    0
Hadamard(I2)
##           [,1]       [,2]
## [1,] 0.7071068  0.7071068
## [2,] 0.7071068 -0.7071068
S1Gate(I2)
##      [,1] [,2]
## [1,] 1+0i 0+0i
## [2,] 0+0i 0-1i
CNOT2_10(I4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    0    0    1
## [3,]    0    0    1    0
## [4,]    0    1    0    0
CNOT3_20(I8)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    0    0    0    0    0    0    0
## [2,]    0    0    0    0    0    1    0    0
## [3,]    0    0    1    0    0    0    0    0
## [4,]    0    0    0    0    0    0    0    1
## [5,]    0    0    0    0    1    0    0    0
## [6,]    0    1    0    0    0    0    0    0
## [7,]    0    0    0    0    0    0    1    0
## [8,]    0    0    0    1    0    0    0    0

3. Compute the inner product of vectors

For example of phi = 1/2|0> + sqrt(3)/2|1> and si= 1/sqrt(2)(10> + |1>) then the inner product is the dot product of the vectors

phi = matrix(c(1/2,sqrt(3)/2),nrow=2,ncol=1)
si = matrix(c(1/sqrt(2),1/sqrt(2)),nrow=2,ncol=1)
angle= innerProduct(phi,si)
cat("Angle between vectors is:",angle)
## Angle between vectors is: 15

4. Compute the dagger function for a gate

The gate dagger computes and displays the transpose of the complex conjugate of the matrix

TGate(I2)
##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068+0.7071068i
GateDagger(TGate(I2))
##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068-0.7071068i

5. Invoking gates in series

The Quantum gates can be chained by passing each preceding Quantum gate as the argument. The final gate in the chain will have the qubit or the identity matrix passed to it.

# Call in reverse order
# Superposition states
# |+> state
Hadamard(q0_)
##           [,1]
## [1,] 0.7071068
## [2,] 0.7071068
# |-> ==> H x Z 
PauliZ(Hadamard(q0_))
##            [,1]
## [1,]  0.7071068
## [2,] -0.7071068
# (+i) Y ==> H x  S 
 SGate(Hadamard(q0_))
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000+0.7071068i
# (-i)Y ==> H x S1
 S1Gate(Hadamard(q0_))
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000-0.7071068i
# Q1 -- TGate- Hadamard
Q1 = Hadamard(TGate(I2))

6. More gates in series

TGate of depth 2

The Quantum circuit for a TGate of Depth 2 is

Q0 — Hadamard-TGate-Hadamard-TGate-SGate-Measurement as shown in IBM’s Quantum Experience Composer

Untitled

Implementing the quantum gates in series in reverse order we have

# Invoking this in reverse order we get
a = SGate(TGate(Hadamard(TGate(Hadamard(q0_)))))
result=measurement(a)

plotMeasurement(result)

fig0-1

7. Invoking gates in parallel

To obtain the results of gates in parallel we have to take the Tensor Product Note:In the TensorProduct invocation the Identity matrix is passed as an argument to get the unitary matrix of the gate. Q0 – Hadamard-Measurement Q1 – Identity- Measurement

# 
a = TensorProd(Hadamard(I2),I2)
b = DotProduct(a,q00_)
plotMeasurement(measurement(b))

fig1-1

a = TensorProd(PauliZ(I2),Hadamard(I2))
b = DotProduct(a,q00_)
plotMeasurement(measurement(b))

fig1-2

8. Measurement

The measurement of a Quantum circuit can be obtained using the measurement function. Consider the following Quantum circuit
Q0 – H-T-H-T-S-H-T-H-T-H-T-H-S-Measurement where H – Hadamard gate, T – T Gate and S- S Gate

a = SGate(Hadamard(TGate(Hadamard(TGate(Hadamard(TGate(Hadamard(SGate(TGate(Hadamard(TGate(Hadamard(I2)))))))))))))
measurement(a)
##          0        1
## v 0.890165 0.109835

9. Plot measurement

Using the same example as above Q0 – H-T-H-T-S-H-T-H-T-H-T-H-S-Measurement where H – Hadamard gate, T – T Gate and S- S Gate we can plot the measurement

a = SGate(Hadamard(TGate(Hadamard(TGate(Hadamard(TGate(Hadamard(SGate(TGate(Hadamard(TGate(Hadamard(I2)))))))))))))
result = measurement(a)
plotMeasurement(result)

fig2-1

10. Evaluating a Quantum Circuit

The above procedures for evaluating a quantum gates in series and parallel can be used to evalute more complex quantum circuits where the quantum gates are in series and in parallel.

Here is an evaluation of one such circuit, the Bell ZQ state using the QCSimulator (from IBM’s Quantum Experience)

pic3

# 1st composite
a = TensorProd(Hadamard(I2),I2)
# Output of CNOT
b = CNOT2_01(a)
# 2nd series
c=Hadamard(TGate(Hadamard(SGate(I2))))
#3rd composite
d= TensorProd(I2,c)
# Output of 2nd composite
e = DotProduct(b,d)
#Action of quantum circuit on |00>
f = DotProduct(e,q00_)
result= measurement(f)
plotMeasurement(result)

fig3-1

11. Toffoli State

This circuit for this comes from IBM’s Quantum Experience. This circuit is available in the package. This is how the state was constructed. This circuit is shown below

pic2

The implementation of the above circuit in QCSimulator is as below

  # Computation of the Toffoli State
    H=1/sqrt(2) * matrix(c(1,1,1,-1),nrow=2,ncol=2)
    I=matrix(c(1,0,0,1),nrow=2,ncol=2)

    # 1st composite
    # H x H x H
    a = TensorProd(TensorProd(H,H),H)
    # 1st CNOT
    a1= CNOT3_12(a)

    # 2nd composite
    # I x I x T1Gate
    b = TensorProd(TensorProd(I,I),T1Gate(I))
    b1 = DotProduct(b,a1)
    c = CNOT3_02(b1)

    # 3rd composite
    # I x I x TGate
    d = TensorProd(TensorProd(I,I),TGate(I))
    d1 = DotProduct(d,c)
    e = CNOT3_12(d1)

    # 4th composite
    # I x I x T1Gate
    f = TensorProd(TensorProd(I,I),T1Gate(I))
    f1 = DotProduct(f,e)
    g = CNOT3_02(f1)

    #5th composite
    # I x T x T
    h = TensorProd(TensorProd(I,TGate(I)),TGate(I))
    h1 = DotProduct(h,g)
    i = CNOT3_12(h1)

    #6th composite
    # I x H x H
    j = TensorProd(TensorProd(I,Hadamard(I)),Hadamard(I))
    j1 = DotProduct(j,i)
    k = CNOT3_12(j1)

    # 7th composite
    # I x H x H
    l = TensorProd(TensorProd(I,Hadamard(I)),Hadamard(I))
    l1 = DotProduct(l,k)
    m = CNOT3_12(l1)
    n = CNOT3_02(m)

    #8th composite
    # T x H x T1
    o = TensorProd(TensorProd(TGate(I),Hadamard(I)),T1Gate(I))
    o1 = DotProduct(o,n)
    p = CNOT3_02(o1)
    result = measurement(p)
    plotMeasurement(result)

fig4-1

12. GHZ YYX measurement

Here is another Quantum circuit, namely the entangled GHZ YYX state. This is

pic1

and is implemented in QCSimulator as

# Composite 1
a = TensorProd(TensorProd(Hadamard(I2),Hadamard(I2)),PauliX(I2))
b= CNOT3_12(a)
c= CNOT3_02(b)
# Composite 2
d= TensorProd(TensorProd(Hadamard(I2),Hadamard(I2)),Hadamard(I2))
e= DotProduct(d,c)
#Composite 3
f= TensorProd(TensorProd(S1Gate(I2),S1Gate(I2)),Hadamard(I2))
g= DotProduct(f,e)
#Composite 4
i= TensorProd(TensorProd(Hadamard(I2),Hadamard(I2)),I2)
j = DotProduct(i,g)
result=measurement(j)
plotMeasurement(result)

fig5-1

Conclusion

The 5 qubit Quantum Computing Simulator is now fully functional. I hope to add more gates and functionality in the months to come.

Feel free to install the package from Github and give it a try.

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

References

  1. IBM’s Quantum Experience
  2. Quantum Computing in Python by Dr. Christine Corbett Moran
  3. Lecture notes-1
  4. Lecture notes-2
  5. Quantum Mechanics and Quantum Computationat edX- UC, Berkeley

My other posts on Quantum Computing

  1. Venturing into IBM’s Quantum Experience 2.Going deeper into IBM’s Quantum Experience!
  2. A primer on Qubits, Quantum gates and Quantum Operations
  3. Exploring Quantum Gate operations with QCSimulator
  4. Taking a closer look at Quantum gates and their operations

You may also like
For more posts on other topics like Cloud Computing, IBM Bluemix, Distributed Computing, OpenCV, R, cricket please check my Index of posts

yorkr ranks ODI batsmen and bowlers


This is the last and final post in which yorkr ranks ODI batsmen and bowlers. These are based on match data from Cricsheet. The ranking is done on

  1. average runs and average strike rate for batsmen and
  2. average wickets and average economy rate for bowlers.

This post has also been published in RPubs RankODIPlayers. You can download this as a pdf file at RankODIPlayers.pdf.

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.

Checkout my interactive Shiny apps GooglyPlus (plots & tables) and Googly (only plots) which can be used to analyze IPL players, teams and matches.

You can take a look at the code at rankODIPlayers (available in yorkr_0.0.5)

rm(list=ls())
library(yorkr)
library(dplyr)
source("rankODIBatsmen.R")
source("rankODIBowlers.R")

Rank ODI batsmen

The top 3 ODI batsmen are hashim Amla (SA), Matther Hayden(Aus) & Virat Kohli (Ind) . Note: For ODI a a cutoff of at least 50 matches played was chosen.

ODIBatsmanRank <- rankODIBatsmen()
as.data.frame(ODIBatsmanRank[1:30,])
##            batsman matches meanRuns    meanSR
## 1          HM Amla     185 51.96216  84.15508
## 2        ML Hayden      79 50.08861  81.20646
## 3          V Kohli     279 48.51971  78.55197
## 4   AB de Villiers     253 47.93676  95.05561
## 5     SR Tendulkar     151 45.82119  79.62311
## 6         S Dhawan     116 45.03448  81.54043
## 7         V Sehwag     167 44.49102 106.27563
## 8          JE Root     111 43.64865  81.66054
## 9        Q de Kock      85 43.61176  82.55235
## 10       IJL Trott     113 43.36283  70.69761
## 11   KC Sangakkara     293 42.81911  75.10420
## 12      TM Dilshan     283 41.76678  89.70360
## 13   KS Williamson     146 41.24658  73.49267
## 14   S Chanderpaul      93 40.07527  70.59613
## 15        HH Gibbs      75 40.00000  79.03813
## 16     Salman Butt      57 39.85965  59.29807
## 17    Anamul Haque      58 39.72414  56.45224
## 18      RT Ponting     238 38.88235  71.94294
## 19       JH Kallis     136 38.77941  67.17794
## 20        MS Dhoni     328 38.57927  90.30555
## 21      MJ Guptill     199 38.54774  73.88090
## 22       DA Warner     138 38.52174  87.24978
## 23 Mohammad Yousuf      94 38.44681  72.69851
## 24        JD Ryder      66 38.40909  91.29667
## 25       GJ Bailey     133 38.38346  75.74519
## 26       G Gambhir     209 37.83254  75.15483
## 27      AJ Strauss     122 37.80328  71.54844
## 28       MJ Clarke     301 37.67442  69.78415
## 29       SR Watson     274 37.08029  83.46489
## 30        AJ Finch     103 36.36893  79.49845

Rank ODI bowlers

The top 3 ODI bowlers are R J Harris (Aus), MJ Henry(NZ) and MA Starc(Aus). Mohammed Shami is 4th and Amit Mishra is 8th A cutoff of 20 matches was considered for bowlers

ODIBowlersRank <- rankODIBowlers()
## [1] 35072     3
## [1] "C:/software/cricket-package/york-test/yorkrData/ODI/ODI-matches"
as.data.frame(ODIBowlersRank[1:30,])
##               bowler matches meanWickets   meanER
## 1  Mustafizur Rahman      56    4.000000 4.293214
## 2           JH Davey      53    3.528302 4.455094
## 3          RJ Harris      94    3.276596 4.361489
## 4           MA Starc     208    3.144231 4.425865
## 5           MJ Henry      88    3.125000 4.961250
## 6         A Flintoff     139    2.956835 4.283022
## 7           A Mishra     106    2.886792 4.365849
## 8     Mohammed Shami     144    2.777778 5.609306
## 9     MJ McClenaghan     165    2.751515 5.640424
## 10          CJ McKay     230    2.704348       NA
## 11       MF Maharoof     114    2.701754 4.427018
## 12       Imran Tahir     156    2.660256 4.461923
## 13        BAW Mendis     234    2.641026 4.532308
## 14     RK Kleinveldt      54    2.629630 4.306667
## 15      Arafat Sunny      62    2.612903 4.103226
## 16         JE Taylor     156    2.602564 5.115192
## 17           AJ Hall      55    2.600000 3.879091
## 18        WD Parnell     129    2.596899 5.477597
## 19         CR Woakes     129    2.596899 5.340620
## 20      DE Bollinger     152    2.592105 4.282763
## 21        Wahab Riaz     206    2.567961 5.431748
## 22        PJ Cummins     148    2.567568 5.715405
## 23         R Rampaul     173    2.549133 4.726590
## 24      Taskin Ahmed      56    2.535714 5.325357
## 25          DW Steyn     292    2.534247 4.534007
## 26      JR Hazlewood      64    2.531250 4.392500
## 27        Abdur Rauf      84    2.523810 4.786667
## 28           SW Tait     141    2.517730 5.173191
## 29      Hamid Hassan     106    2.509434 4.686038
## 30        SL Malinga     419    2.498807 4.968974

Hope you have fun with my yorkr package.!

yorkr crashes the IPL party! – Part 2


Most people say that it is the intellect which makes a great scientist. They are wrong: it is character.

                 Albert Einstein

*Science is organized knowledge. Wisdom is organized life.“*

                 Immanuel Kant

If I have seen further, it is by standing on the shoulders of giants

                 Isaac Newton
                 

Valid criticism does you a favor.

                 Carl Sagan

Introduction

In this post, my R package ‘yorkr’, continues to bat in the IPL Twenty20s. This post is a continuation of my earlier post – yorkr crashes the IPL party ! – Part 1. This post deals with Class 2 functions namely the performances of an IPL team in all T20 matches against another IPL team for e.g all T20 matches of Chennai Super Kings vs Royal Challengers Bangalore or Kochi Tuskers Kerala vs Mumbai Indians etc.

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

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

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.

Checkout my interactive Shiny apps GooglyPlus (plots & tables) and Googly (only plots) which can be used to analyze IPL players, teams and matches.

The list of function in Class 2 are

  1. teamBatsmenPartnershiOppnAllMatches()
  2. teamBatsmenPartnershipOppnAllMatchesChart()
  3. teamBatsmenVsBowlersOppnAllMatches()
  4. teamBattingScorecardOppnAllMatches()
  5. teamBowlingPerfOppnAllMatches()
  6. teamBowlersWicketsOppnAllMatches()
  7. teamBowlersVsBatsmenOppnAllMatches()
  8. teamBowlersWicketKindOppnAllMatches()
  9. teamBowlersWicketRunsOppnAllMatches()
  10. plotWinLossBetweenTeams()

1. Install the package from CRAN

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

2. Get data for all T20 matches between 2 teams

We can get all IPL T20 matches between any 2 teams using the function below. The dir parameter should point to the folder which has the IPL T20 RData files of the individual matches. This function creates a data frame of all the IPL T20 matches and also saves the dataframe as RData. The function below gets all matches between India and Australia

setwd("C:/software/cricket-package/york-test/yorkrData/IPL/IPL-T20-matches")
matches <- getAllMatchesBetweenTeams("Sunrisers Hyderabad","Royal Challengers Bangalore",dir=".")
dim(matches)
## [1] 1320   25

I have however already saved the IPL Twenty20 matches for all possible combinations of opposing IPL Teams. The data for these matches for the individual teams/countries can be obtained from Github at in the folder IPL-T20-allmatches-between-two-teams

Note: You will need to use the function below for future matches! The data in Cricsheet are from 2008 -2015

3. Save data for all matches between all combination of 2 teams

This can be done locally using the function below. You could use this function to combine all IPL Twenty20 matches between any 2 IPL teams into a single dataframe and save it in the current folder. The current implementation expects that the the RData files of individual matches are in ../data folder. Since I already have converted this I will not be running this again

# Available in yorkr_0.0.5. Can be installed from Github though!
#saveAllMatchesBetween2IPLTeams()

4. Load data directly for all matches between 2 IPL teams

As in my earlier post I pick all IPL Twenty20 matches between 2 random IPL teams. I load the data directly from the stored RData files. When we load the Rdata file a “matches” object will be created. This object can be stored for the apporpriate teams as below

# Load T20 matches between 2 IPL teams
setwd("C:/software/cricket-package/york-test/yorkrData/IPL/IPL-T20-allmatches-between-two-teams")
load("Chennai Super Kings-Delhi Daredevils-allMatches.RData")
csk_dd_matches <- matches
load("Deccan Chargers-Kolkata Knight Riders-allMatches.RData")
dc_kkr_matches <- matches
load("Mumbai Indians-Pune Warriors-allMatches.RData")
mi_pw_matches <- matches
load("Rajasthan Royals-Sunrisers Hyderabad-allMatches.RData")
rr_sh_matches <- matches
load("Kings XI Punjab-Royal Challengers Bangalore-allMatches.RData")
kxip_rcb_matches <-matches
load("Chennai Super Kings-Kochi Tuskers Kerala-allMatches.RData")
csk_ktk_matches <-matches

5. Team Batsmen partnership in Twenty20 (all matches with opposing IPL team)

This function will create a report of the batting partnerships in the IPL teams for the matches between the teams. The report can be brief or detailed depending on the parameter ‘report’. As can be seen M S Dhoni tops the list for CSK, followed by Raina and then Murali Vijay for matches against Delhi Daredevils. For the Delhi Daredevils it is V Sehawag followed by Gambhir.

m<- teamBatsmenPartnershiOppnAllMatches(csk_dd_matches,'Chennai Super Kings',report="summary")
m
## Source: local data frame [29 x 2]
## 
##         batsman totalRuns
##          (fctr)     (dbl)
## 1      MS Dhoni       364
## 2      SK Raina       335
## 3       M Vijay       290
## 4   S Badrinath       185
## 5     ML Hayden       181
## 6    MEK Hussey       169
## 7  F du Plessis       100
## 8      S Vidyut        94
## 9      DR Smith        81
## 10    JA Morkel        80
## ..          ...       ...
m<- teamBatsmenPartnershiOppnAllMatches(csk_dd_matches,'Delhi Daredevils',report="summary")
m
## Source: local data frame [53 x 2]
## 
##             batsman totalRuns
##              (fctr)     (dbl)
## 1          V Sehwag       233
## 2         G Gambhir       200
## 3         DA Warner       134
## 4    AB de Villiers       133
## 5        KD Karthik       129
## 6  DPMD Jayawardene        89
## 7         JA Morkel        81
## 8        TM Dilshan        79
## 9          S Dhawan        78
## 10          SS Iyer        77
## ..              ...       ...
m <-teamBatsmenPartnershiOppnAllMatches(dc_kkr_matches,'Deccan Chargers',report="summary")
m
## Source: local data frame [29 x 2]
## 
##            batsman totalRuns
##             (fctr)     (dbl)
## 1     AC Gilchrist       166
## 2         HH Gibbs       145
## 3        RG Sharma       116
## 4         S Dhawan       111
## 5        A Symonds       100
## 6  Y Venugopal Rao        92
## 7         B Chipli        60
## 8     DB Ravi Teja        54
## 9         TL Suman        53
## 10      VVS Laxman        32
## ..             ...       ...
m <-teamBatsmenPartnershiOppnAllMatches(mi_pw_matches,'Mumbai Indians',report="detailed")
m[1:30,]
##         batsman   nonStriker partnershipRuns totalRuns
## 1  SR Tendulkar JEC Franklin              24       152
## 2  SR Tendulkar    AT Rayudu              46       152
## 3  SR Tendulkar    RG Sharma               2       152
## 4  SR Tendulkar   KD Karthik              20       152
## 5  SR Tendulkar   RT Ponting              39       152
## 6  SR Tendulkar  AC Blizzard              12       152
## 7  SR Tendulkar  RJ Peterson               9       152
## 8     RG Sharma SR Tendulkar               3       135
## 9     RG Sharma JEC Franklin               0       135
## 10    RG Sharma    AT Rayudu              34       135
## 11    RG Sharma    A Symonds              19       135
## 12    RG Sharma   KD Karthik              19       135
## 13    RG Sharma   KA Pollard              47       135
## 14    RG Sharma     TL Suman               7       135
## 15    RG Sharma   GJ Maxwell               6       135
## 16   KD Karthik SR Tendulkar               8       108
## 17   KD Karthik JEC Franklin              32       108
## 18   KD Karthik    AT Rayudu               3       108
## 19   KD Karthik    RG Sharma              50       108
## 20   KD Karthik   SL Malinga              10       108
## 21   KD Karthik      PP Ojha               0       108
## 22   KD Karthik  RJ Peterson               4       108
## 23   KD Karthik  NLTC Perera               1       108
## 24    AT Rayudu SR Tendulkar              54        92
## 25    AT Rayudu    RG Sharma              37        92
## 26    AT Rayudu   KD Karthik               1        92
## 27 JEC Franklin SR Tendulkar              31        63
## 28 JEC Franklin    RG Sharma               1        63
## 29 JEC Franklin   KD Karthik              15        63
## 30 JEC Franklin     SA Yadav              10        63
m <-teamBatsmenPartnershiOppnAllMatches(rr_sh_matches,'Sunrisers Hyderabad',report="summary")
m
## Source: local data frame [23 x 2]
## 
##         batsman totalRuns
##          (fctr)     (dbl)
## 1      S Dhawan       168
## 2     DJG Sammy        95
## 3    EJG Morgan        90
## 4     DA Warner        83
## 5       NV Ojha        50
## 6      KL Rahul        40
## 7     RS Bopara        40
## 8      DW Steyn        31
## 9      CL White        31
## 10 MC Henriques        29
## ..          ...       ...
m <-teamBatsmenPartnershiOppnAllMatches(kxip_rcb_matches,'Kings XI Punjab',report="summary")
m
## Source: local data frame [47 x 2]
## 
##          batsman totalRuns
##           (fctr)     (dbl)
## 1       SE Marsh       246
## 2      DA Miller       224
## 3      RS Bopara       203
## 4   AC Gilchrist       191
## 5   Yuvraj Singh       126
## 6       MS Bisla       103
## 7  Mandeep Singh       100
## 8      DJ Hussey        99
## 9  Azhar Mahmood        96
## 10 KC Sangakkara        88
## ..           ...       ...
m <-teamBatsmenPartnershiOppnAllMatches(csk_ktk_matches,'Kochi Tuskers Kerala',report="summary")
m
## Source: local data frame [8 x 2]
## 
##            batsman totalRuns
##             (fctr)     (dbl)
## 1      BB McCullum        80
## 2         BJ Hodge        70
## 3         PA Patel        40
## 4        RA Jadeja        35
## 5 Y Gnaneswara Rao        19
## 6 DPMD Jayawardene        16
## 7          OA Shah         3
## 8        KM Jadhav         1

6. Team batsmen partnership in Twenty20 (all matches with opposing IPL team)

This is plotted graphically in the charts below. The partnerships are shown. Note: All functions which create a plot also include a parameter plot=TRUE/FALSE. If you set this as FALSE then a data frame is returned. You can use the dataframe to create an interactive plot for the partnerships (mouse over) using packages like plotly,rcharts, googleVis or ggvis.

teamBatsmenPartnershipOppnAllMatchesChart(csk_dd_matches,'Chennai Super Kings',"Delhi Daredevils")

teamBatsmenPartnership-1

teamBatsmenPartnershipOppnAllMatchesChart(dc_kkr_matches,main="Kolkata Knight Riders",opposition="Deccan Chargers")

teamBatsmenPartnership-2

teamBatsmenPartnershipOppnAllMatchesChart(kxip_rcb_matches,"Royal Challengers Bangalore",opposition="Kings XI Punjab")

teamBatsmenPartnership-3

teamBatsmenPartnershipOppnAllMatchesChart(mi_pw_matches,"Mumbai Indians","Pune Warriors")

teamBatsmenPartnership-4

m <- teamBatsmenPartnershipOppnAllMatchesChart(rr_sh_matches,"Rajasthan Royals","Sunrisers Hyderabad",plot=FALSE)
m[1:30,]
##        batsman  nonStriker runs
## 1    SR Watson   STR Binny   60
## 2    AM Rahane   STR Binny   59
## 3    STR Binny   AM Rahane   45
## 4    SR Watson    R Dravid   42
## 5    AM Rahane   SV Samson   41
## 6     BJ Hodge   SV Samson   36
## 7    CH Morris   STR Binny   34
## 8    AM Rahane   SR Watson   31
## 9     R Dravid   SR Watson   30
## 10   SV Samson   AM Rahane   29
## 11   SR Watson   AM Rahane   27
## 12   SPD Smith    DJ Hooda   25
## 13   SPD Smith JP Faulkner   24
## 14   SPD Smith   STR Binny   20
## 15    R Dravid   AM Rahane   18
## 16    BJ Hodge JP Faulkner   18
## 17 JP Faulkner   SPD Smith   18
## 18   SV Samson     KK Nair   14
## 19 JP Faulkner   STR Binny   14
## 20   SV Samson   STR Binny   13
## 21   SPD Smith   AM Rahane   13
## 22   SR Watson   SPD Smith   12
## 23   STR Binny JP Faulkner   12
## 24   STR Binny   SPD Smith   12
## 25 JP Faulkner   SV Samson   12
## 26     KK Nair   SV Samson   12
## 27 JP Faulkner    BJ Hodge   11
## 28   SPD Smith   SR Watson   10
## 29   STR Binny   SR Watson    9
## 30   SV Samson    BJ Hodge    9

7. Team batsmen versus bowler in Twenty20 (all matches with opposing IPL team)

The plots below provide information on how each of the top batsmen of the IPL teams fared against the opposition bowlers

# Adam Gilchrist was the top performer for Deccan Chargers
teamBatsmenVsBowlersOppnAllMatches(dc_kkr_matches,"Deccan Chargers","Kolkata Knight Riders")

batsmenvsBowler-1

teamBatsmenVsBowlersOppnAllMatches(csk_dd_matches,"Delhi Daredevils","Chennai Super Kings",top=3)

batsmenvsBowler-2

m <- teamBatsmenVsBowlersOppnAllMatches(csk_ktk_matches,"Chennai Super Kings","Kochi Tuskers Kerala",top=10,plot=FALSE)
m
## Source: local data frame [37 x 3]
## Groups: batsman [1]
## 
##     batsman         bowler  runs
##      (fctr)         (fctr) (dbl)
## 1  SK Raina       RP Singh     6
## 2  SK Raina    S Sreesanth    18
## 3  SK Raina M Muralitharan     1
## 4  SK Raina  R Vinay Kumar     4
## 5  SK Raina    NLTC Perera    11
## 6  SK Raina       RR Powar    13
## 7  SK Raina       RV Gomez    16
## 8   WP Saha       RP Singh    15
## 9   WP Saha M Muralitharan    11
## 10  WP Saha       BJ Hodge     1
## ..      ...            ...   ...
teamBatsmenVsBowlersOppnAllMatches(rr_sh_matches,"Sunrisers Hyderabad","Rajasthan Royals")

batsmenvsBowler-3

8. Team batsmen versus bowler in Twenty20(all matches with opposing IPL team)

The following tables gives the overall performances of the IPL team’s batsmen against the opposition.

#Chris Gayle followed by Virat Kohli tops for RCB
a <-teamBattingScorecardOppnAllMatches(kxip_rcb_matches,main="Royal Challengers Bangalore",opposition="Kings XI Punjab")
## Total= 2444
a
## Source: local data frame [55 x 5]
## 
##           batsman ballsPlayed fours sixes  runs
##            (fctr)       (int) (int) (int) (dbl)
## 1        CH Gayle         313    45    41   561
## 2         V Kohli         296    39     8   344
## 3  AB de Villiers         183    23    16   301
## 4       JH Kallis         133    18     7   187
## 5        R Dravid          90    11     1   105
## 6      RV Uthappa          47     7     6    92
## 7       CA Pujara          66    11    NA    70
## 8       MK Pandey          50     5     3    67
## 9    KP Pietersen          43     7     1    66
## 10     MV Boucher          36     4     1    41
## ..            ...         ...   ...   ...   ...
#Tendulkar & Rohit Sharma lead for Mumbai Indians
teamBattingScorecardOppnAllMatches(mi_pw_matches,"Mumbai Indians","Pune Warriors")
## Total= 756
## Source: local data frame [20 x 5]
## 
##            batsman ballsPlayed fours sixes  runs
##             (fctr)       (int) (int) (int) (dbl)
## 1     SR Tendulkar         134    21     1   152
## 2        RG Sharma         121     7     6   135
## 3       KD Karthik         107    10     3   108
## 4        AT Rayudu          93     8     1    92
## 5     JEC Franklin          70     5     2    63
## 6       KA Pollard          43     3     3    55
## 7         TL Suman          16     3     3    36
## 8  Harbhajan Singh          22     3     1    29
## 9       SL Malinga          16     2     1    19
## 10       A Symonds          18     2    NA    19
## 11      RT Ponting          17     2    NA    14
## 12      GJ Maxwell           7     1     1    13
## 13     RJ Peterson          13     1    NA    13
## 14     AC Blizzard           6     1    NA     6
## 15         PP Ojha           2    NA    NA     1
## 16        MM Patel           2    NA    NA     1
## 17         RE Levi           2    NA    NA     0
## 18        SA Yadav           4    NA    NA     0
## 19     NLTC Perera           4    NA    NA     0
## 20        DR Smith           1    NA    NA     0
teamBattingScorecardOppnAllMatches(mi_pw_matches,"Pune Warriors","Mumbai Indians")
## Total= 714
## Source: local data frame [28 x 5]
## 
##         batsman ballsPlayed fours sixes  runs
##          (fctr)       (int) (int) (int) (dbl)
## 1    RV Uthappa         131    13     4   151
## 2     MK Pandey          80     5     4    88
## 3  Yuvraj Singh          62     3     6    77
## 4      M Manhas          36     5    NA    42
## 5     SPD Smith          38     4    NA    41
## 6      MR Marsh          26     2     2    38
## 7      M Kartik          21     2     1    25
## 8      R Sharma          22     2     1    23
## 9      TL Suman          15     5    NA    23
## 10   WD Parnell          24     3    NA    22
## ..          ...         ...   ...   ...   ...
teamBattingScorecardOppnAllMatches(csk_dd_matches,"Delhi Daredevils","Chennai Super Kings")
## Total= 1983
## Source: local data frame [53 x 5]
## 
##             batsman ballsPlayed fours sixes  runs
##              (fctr)       (int) (int) (int) (dbl)
## 1          V Sehwag         147    27     9   233
## 2         G Gambhir         155    23     2   200
## 3         DA Warner         130    11     2   134
## 4    AB de Villiers          80     7     6   133
## 5        KD Karthik          99    15     1   129
## 6  DPMD Jayawardene          77     7     2    89
## 7         JA Morkel          63     8     2    81
## 8        TM Dilshan          65     8     3    79
## 9          S Dhawan          58     8     2    78
## 10          SS Iyer          56    11     1    77
## ..              ...         ...   ...   ...   ...
teamBattingScorecardOppnAllMatches(rr_sh_matches,"Rajasthan Royals","Sunrisers Hyderabad")
## Total= 808
## Source: local data frame [17 x 5]
## 
##          batsman ballsPlayed fours sixes  runs
##           (fctr)       (int) (int) (int) (dbl)
## 1      SR Watson          97    22     4   148
## 2      AM Rahane         145    17     1   148
## 3      SPD Smith          81    11     2   103
## 4      STR Binny          83     6     1    90
## 5      SV Samson          83     3     4    76
## 6    JP Faulkner          41     7     2    59
## 7       BJ Hodge          37     2     5    55
## 8       R Dravid          44     7     1    48
## 9      CH Morris          11     2     3    34
## 10       KK Nair          23     3    NA    17
## 11      R Bhatia          10     1    NA     8
## 12   DS Kulkarni           6     1    NA     7
## 13      DJ Hooda           9    NA    NA     7
## 14      AM Nayar           3     1    NA     4
## 15      PV Tambe           7    NA    NA     3
## 16 KW Richardson           2    NA    NA     1
## 17     DH Yagnik           4    NA    NA     0

9. Team performances of IPL bowlers (all matches with opposing IPL team)

Like the function above the following tables provide the top IPL bowlers of the respective teams in the matches against the opposition.

#Piyush Chawla has the most wickets for KXIP against RCB
teamBowlingPerfOppnAllMatches(kxip_rcb_matches,"Kings XI Punjab","Royal Challengers Bangalore")
## Source: local data frame [38 x 5]
## 
##            bowler overs maidens  runs wickets
##            (fctr) (int)   (int) (dbl)   (dbl)
## 1       PP Chawla    14       0   311      12
## 2       IK Pathan    12       0   159       9
## 3      YA Abdulla     9       1   103       8
## 4       RJ Harris     5       0    87       7
## 5         P Awana    11       0   149       6
## 6     S Sreesanth     6       0   101       5
## 7   Azhar Mahmood     8       0    74       5
## 8  Sandeep Sharma     8       1   101       4
## 9        AR Patel     5       0    94       4
## 10      VRV Singh     6       0    70       4
## ..            ...   ...     ...   ...     ...
#Ashwin is the highest wicket takes for CSK against DD
teamBowlingPerfOppnAllMatches(csk_dd_matches,main="Chennai Super Kings",opposition="Delhi Daredevils")
## Source: local data frame [26 x 5]
## 
##           bowler overs maidens  runs wickets
##           (fctr) (int)   (int) (dbl)   (dbl)
## 1       R Ashwin     9       0   233      17
## 2      JA Morkel    11       0   338      10
## 3       DJ Bravo     5       0   135       8
## 4      SB Jakati     4       0   140       6
## 5       L Balaji    10       0   117       6
## 6      MM Sharma     1       0    99       6
## 7      RA Jadeja     2       0    85       4
## 8      IC Pandey     1       0    80       4
## 9  BW Hilfenhaus     5       0    53       4
## 10       A Nehra     1       0    25       4
## ..           ...   ...     ...   ...     ...
teamBowlingPerfOppnAllMatches(dc_kkr_matches,"Deccan Chargers","Kolkata Knight Riders")
## Source: local data frame [26 x 5]
## 
##            bowler overs maidens  runs wickets
##            (fctr) (int)   (int) (dbl)   (dbl)
## 1        RP Singh    11       0   161       7
## 2         PP Ojha    11       0   196       6
## 3      WPUJC Vaas     4       0    67       5
## 4       A Symonds    12       0   100       4
## 5        DW Steyn     8       0    88       4
## 6        A Mishra     8       0    68       3
## 7  Jaskaran Singh     6       0    53       3
## 8       SB Styris     7       0    79       2
## 9       RJ Harris     4       0    20       2
## 10  Harmeet Singh    10       0    84       1
## ..            ...   ...     ...   ...     ...

10. Team bowler’s wickets in IPL Twenty20 (all matches with opposing IPL team)

This provided a graphical plot of the tables above

# Dirk Nannes and Umesh Yadav top for DD against CSK
teamBowlersWicketsOppnAllMatches(csk_dd_matches,"Delhi Daredevils","Chennai Superkings")

bowlerWicketsOppn-1

# SL Malinga and Munaf Patel lead in MI vs PW clashes
teamBowlersWicketsOppnAllMatches(mi_pw_matches,"Mumbai Indians","Pune Warrors")

bowlerWicketsOppn-2

teamBowlersWicketsOppnAllMatches(dc_kkr_matches,"Kolkata Knight Riders","Deccan Chargers",top=10) 

bowlerWicketsOppn-3

m <-teamBowlersWicketsOppnAllMatches(kxip_rcb_matches,"Royal Challengers Bangalore","Kings XI Punjab",plot=FALSE)
m
## Source: local data frame [20 x 2]
## 
##              bowler wickets
##              (fctr)   (int)
## 1         S Aravind       8
## 2            Z Khan       7
## 3          MA Starc       7
## 4          HV Patel       6
## 5           P Kumar       5
## 6         YS Chahal       5
## 7         JH Kallis       4
## 8     R Vinay Kumar       3
## 9          A Kumble       3
## 10         CH Gayle       3
## 11      AB McDonald       3
## 12         VR Aaron       3
## 13         DW Steyn       2
## 14    CK Langeveldt       2
## 15       DL Vettori       2
## 16         M Kartik       2
## 17 RE van der Merwe       2
## 18        R Rampaul       1
## 19        JA Morkel       1
## 20         AB Dinda       1

11. Team bowler vs batsmen in Twenty20(all matches with opposing IPL team)

These plots show how the IPL bowlers fared against the batsmen. It shows which of the opposing IPL teams batsmen were able to score the most runs

teamBowlersVsBatsmenOppnAllMatches(rr_sh_matches,'Rajasthan Royals',"Sunrisers Hyderabd",top=5)

bowlerVsBatsmen-1

teamBowlersVsBatsmenOppnAllMatches(kxip_rcb_matches,"Kings XI Punjab","Royal Challengers Bangalore",top=3)

bowlerVsBatsmen-2

teamBowlersVsBatsmenOppnAllMatches(dc_kkr_matches,"Deccan Chargers","Kolkata Knight Riders")

bowlerVsBatsmen-3

12. Team bowler’s wicket kind in Twenty20(caught,bowled,etc) (all matches with opposing IPL team)

The charts below show the wicket kind taken by the bowler of the IPL team(caught, bowled, lbw etc)

teamBowlersWicketKindOppnAllMatches(csk_dd_matches,"Delhi Daredevils","Chennai Super Kings",plot=TRUE)

bowlerWickets-1

m <- teamBowlersWicketKindOppnAllMatches(mi_pw_matches,"Pune Warriors","Mumbai Indians",plot=FALSE)
m[1:30,]
##          bowler wicketKind wicketPlayerOut runs
## 1       SB Wagh     caught    JEC Franklin   31
## 2      R Sharma     caught    SR Tendulkar   64
## 3     AC Thomas     caught       AT Rayudu   69
## 4      M Kartik    stumped         RE Levi   70
## 5      AB Dinda     caught       AT Rayudu  150
## 6      AB Dinda     caught       RG Sharma  150
## 7      M Kartik    stumped      KD Karthik   70
## 8    MN Samuels     bowled        SA Yadav   21
## 9      R Sharma     bowled      KA Pollard   64
## 10     AB Dinda     caught    JEC Franklin  150
## 11   WD Parnell     caught      SL Malinga   64
## 12     AB Dinda        lbw Harbhajan Singh  150
## 13 Yuvraj Singh     caught      RT Ponting   61
## 14     AJ Finch     caught    SR Tendulkar   11
## 15     MR Marsh        lbw      KD Karthik   24
## 16    AC Thomas     caught     AC Blizzard   69
## 17 Yuvraj Singh     caught    SR Tendulkar   61
## 18 Yuvraj Singh     caught       AT Rayudu   61
## 19     R Sharma     caught       RG Sharma   64
## 20     R Sharma     caught        TL Suman   64
## 21    JE Taylor     caught       A Symonds   34
## 22    JE Taylor     caught      KA Pollard   34
## 23      B Kumar     caught    JEC Franklin   50
## 24    MJ Clarke    run out       RG Sharma    9
## 25      A Nehra     caught    SR Tendulkar   19
## 26      A Nehra     caught     RJ Peterson   19
## 27      B Kumar     bowled       AT Rayudu   50
## 28      A Nehra    run out     NLTC Perera   19
## 29     AB Dinda     caught Harbhajan Singh  150
## 30   WD Parnell    run out      SL Malinga   64
teamBowlersWicketKindOppnAllMatches(dc_kkr_matches,"Kolkata Knight Riders",'Deccan Chargers',plot=TRUE)

bowlerWickets-2

13. Team bowler’s wicket taken and runs conceded in Twenty20(all matches with opposing IPL team)

teamBowlersWicketRunsOppnAllMatches(csk_ktk_matches,"Kochi Tuskers Kerala","Chennai Super Kings")

wicketRuns-1

m <-teamBowlersWicketRunsOppnAllMatches(mi_pw_matches,"Mumbai Indians","Pune Warriors",plot=FALSE)
m[1:30,]
## Source: local data frame [30 x 5]
## 
##             bowler overs maidens  runs wickets
##             (fctr) (int)   (int) (dbl)   (dbl)
## 1       AG Murtaza     4       0    18       2
## 2       SL Malinga     9       1   143      10
## 3         AN Ahmed     5       0    40       4
## 4         MM Patel     6       1    88       7
## 5       KA Pollard     6       0    99       5
## 6     JEC Franklin     4       0    64       1
## 7  Harbhajan Singh     7       0    85       6
## 8          PP Ojha     8       0    95       4
## 9       MG Johnson     5       0    41       4
## 10        R Dhawan     1       0    27       0
## ..             ...   ...     ...   ...     ...

14. Plot of wins vs losses between teams in IPL T20 confrontations

setwd("C:/software/cricket-package/york-test/yorkrData/IPL/IPL-T20-matches")
plotWinLossBetweenTeams("Chennai Super Kings","Delhi Daredevils")

winsLosses-1

plotWinLossBetweenTeams("Deccan Chargers","Kolkata Knight Riders",".")

winsLosses-2

plotWinLossBetweenTeams('Kings XI Punjab',"Royal Challengers Bangalore",".")

winsLosses-3

plotWinLossBetweenTeams("Mumbai Indians","Pune Warriors",".")

winsLosses-4

plotWinLossBetweenTeams('Rajasthan Royals',"Sunrisers Hyderabad",".")

winsLosses-5

plotWinLossBetweenTeams('Chennai Super Kings',"Mumbai Indians",".")

winsLosses-6

Conclusion

This post included all functions for all IPL Twenty20 matches between any 2 IPL teams. As before the data frames are already available. You can load the data and begin to use them. If more insights from the dataframe are possible do go ahead. But please do attribute the source to Cricheet (http://cricsheet.org), my package yorkr and my blog. Do give the functions a spin for yourself!

You may also like

  1. yorkr pads up for the Twenty20s: Part 1- Analyzing team“s match performance
  2. yorkr pads up for the Twenty20s:Part 4- Individual batting and bowling performances
  3. Introducing cricket package yorkr: Part 2-Trapped leg before wicket!
  4. Introducing cricket package yorkr:Part 4-In the block hole!
  5. Introducing cricketr! : An R package to analyze performances of cricketers
  6. Cricket analytics with cricketr
  7. OpenCV: Fun with filters and convolution
  8. To Hadoop, or not to Hadoop
  9. Close encounters with the future
  10. Presentation on ‘Evolution to LTE’

yorkr crashes the IPL party ! – Part 1


 

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

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.

Checkout my interactive Shiny apps GooglyPlus (plots & tables) and Googly (only plots) which can be used to analyze IPL players, teams and matches.

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

yorkr pads up for Twenty20s:Part 4- Individual batting and bowling performances!


Introduction

In theory, theory and practice are the same. In practice, they’re not.

                      Yogi Berra

There are two ways to write error-free programs; only the third one works.

                      Alan Perlis

Simplicity does not precede complexity, but follows it.

                      Alan Perlis

Talk is cheap. Show me the code.

                      Linus Torvalds

This post is the 4th and the last part of yorkr padding for the Twenty20s. In this post I look at the top individual batting and bowling performances in the Twenty20s. Also please take a look at my 3 earlier post on yorkr’s handling of Twenty20 matches

  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!

The 1st part included functions dealing with a specific T20 match, the 2nd part dealt with functions between 2 opposing teams in T20 confrontations. The 3rd part dealt with functions between a team and all T20 matches with all oppositions. This 4th part includes individual batting and bowling performances in T20 matches and deals with Class 4 functions.

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

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

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

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.

Checkout my interactive Shiny apps GooglyPlus (plots & tables) and Googly (only plots) which can be used to analyze IPL players, teams and matches.

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

Note: The yorkr package in its current avatar only supports ODI & Twenty20 matches. I will be upgrading the package to handle IPL in the months to come.

library(yorkr)
library(gridExtra)
library(rpart.plot)
library(dplyr)
library(ggplot2)
rm(list=ls())

A. Batsman functions

1. Get Team Batting details

The function below gets the overall team batting details based on the RData file available in T20 matches. This is currently also available in Github at [yorkrData] (https://github.com/tvganesh/yorkrData/tree/master/Twenty20/T20-matches). The batting details of the 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/Twenty20/T20-matches")
india_details <- getTeamBattingDetails("India",dir=".", save=TRUE)
sa_details <- getTeamBattingDetails("South Africa",dir=".",save=TRUE)
nz_details <- getTeamBattingDetails("New Zealand",dir=".",save=TRUE)
eng_details <- getTeamBattingDetails("England",dir=".",save=TRUE)
pak_details <- getTeamBattingDetails("Pakistan",dir=".",save=TRUE)
aus_details <- getTeamBattingDetails("Australia",dir=".",save=TRUE)
wi_details <- getTeamBattingDetails("West Indies",dir=".",save=TRUE)

2. Get batsman details

This function is used to get the individual T20 batting record for a the specified batsman of the country as in the functions below. For analyzing the batting performances the top T20 batsmen from different countries have been chosen. The batting scorecard functions from yorkr pads up for the Twenty20s:Part 3:Overall team performance against all oppositions! was used for selecting these batsmen

  1. Virat Kohli (Ind)
  2. DA Warner (Aus)
  3. Umar Akmal (Pak)
  4. BB McCullum (NZ)
  5. EJG Morgan (Eng)
  6. CH Gayle (WI)
setwd("C:/software/cricket-package/cricsheet/cleanup/T20/rmd/part4")
kohli <- getBatsmanDetails(team="India",name="Kohli",dir=".")
## [1] "./India-BattingDetails.RData"
warner <- getBatsmanDetails(team="Australia",name="DA Warner")
## [1] "./Australia-BattingDetails.RData"
akmal <-  getBatsmanDetails(team="Pakistan",name="Umar Akmal",dir=".")
## [1] "./Pakistan-BattingDetails.RData"
mccullum <-  getBatsmanDetails(team="New Zealand",name="BB McCullum",dir=".")
## [1] "./New Zealand-BattingDetails.RData"
emorgan <-  getBatsmanDetails(team="England",name="EJG Morgan",dir=".")
## [1] "./England-BattingDetails.RData"
gayle <-  getBatsmanDetails(team="West Indies",name="CH Gayle",dir=".")
## [1] "./West Indies-BattingDetails.RData"

3. Runs versus deliveries

Chris Gayle and B McCullum have an astounding strike rate and touch close to 120 runs in 60 balls. David Warner also has a great strike rate

p1 <-batsmanRunsVsDeliveries(kohli,"Kohli")
p2 <-batsmanRunsVsDeliveries(warner, "DA Warner")
p3 <-batsmanRunsVsDeliveries(akmal,"U Akmal")
p4 <-batsmanRunsVsDeliveries(mccullum,"BB McCullum")
p5 <-batsmanRunsVsDeliveries(emorgan,"EJG Morgan")
p6 <-batsmanRunsVsDeliveries(gayle,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

runsVsDeliveries-1

4. Batsman Total runs, Fours and Sixes

The plots below show the total runs, fours and sixes by the batsmen. Gayle tops in the runs from sixes

kohli46 <- select(kohli,batsman,ballsPlayed,fours,sixes,runs)
p1 <- batsmanFoursSixes(kohli46,"Kohli")
warner46 <- select(warner,batsman,ballsPlayed,fours,sixes,runs)
p2 <- batsmanFoursSixes(warner46,"DA Warner")
akmal46 <- select(akmal,batsman,ballsPlayed,fours,sixes,runs)
p3 <- batsmanFoursSixes(akmal46, "U Akmal")
mccullum46 <- select(mccullum,batsman,ballsPlayed,fours,sixes,runs)
p4 <- batsmanFoursSixes(mccullum46,"BB McCullum")
emorgan46 <- select(emorgan,batsman,ballsPlayed,fours,sixes,runs)
p5 <- batsmanFoursSixes(emorgan46,"EJG Morgan")
gayle46 <- select(gayle,batsman,ballsPlayed,fours,sixes,runs)
p6 <- batsmanFoursSixes(gayle46,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

foursSixes-1

5. Batsman dismissals

The type of dismissal for each batsman is shown below

p1 <-batsmanDismissals(kohli,"Kohli")
p2 <-batsmanDismissals(warner, "DA Warner")
p3 <-batsmanDismissals(akmal,"U Akmal")
p4 <-batsmanDismissals(mccullum,"BB McCullum")
p5 <-batsmanDismissals(emorgan,"EJG Morgan")
p6 <-batsmanDismissals(gayle,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

dismissal-1

6. Runs versus Strike Rate

Gayle’s and McCullum’s strike rate touch 120% for runs in the range of 130-150

p1 <-batsmanRunsVsStrikeRate(kohli,"Kohli")
p2 <-batsmanRunsVsStrikeRate(warner, "DA Warner")
p3 <-batsmanRunsVsStrikeRate(akmal,"U Akmal")
p4 <-batsmanRunsVsStrikeRate(mccullum,"BB McCullum")
p5 <-batsmanRunsVsStrikeRate(emorgan,"EJG Morgan")
p6 <-batsmanRunsVsStrikeRate(gayle,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

runsSR-1

7. Batsman moving average

Kohli and Gayle T20 average is on the increase touching 50. Eoin Morgan and BB McCullum average around 40.

p1 <-batsmanMovingAverage(kohli,"Kohli")
p2 <-batsmanMovingAverage(warner, "DA Warner")
p3 <-batsmanMovingAverage(akmal,"U Akmal")
p4 <-batsmanMovingAverage(mccullum,"BB McCullum")
p5 <-batsmanMovingAverage(emorgan,"EJG Morgan")
p6 <-batsmanMovingAverage(gayle,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

ma-1

8. Batsman cumulative average

Kohli’s cumulative average steadies around 40, McCullum shows a gentle decline from 40+ to 35+. Gayle oscillates between 30+ to 40-.

p1 <-batsmanCumulativeAverageRuns(kohli,"Kohli")
p2 <-batsmanCumulativeAverageRuns(warner, "DA Warner")
p3 <-batsmanCumulativeAverageRuns(akmal,"U Akmal")
p4 <-batsmanCumulativeAverageRuns(mccullum,"BB McCullum")
p5 <-batsmanCumulativeAverageRuns(emorgan,"EJG Morgan")
p6 <-batsmanCumulativeAverageRuns(gayle,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cAvg-1

9. Cumulative Average Strike Rate

BB McCullum has the best overall cumulative strike rate which hovered around the 150 and steadies around 130. Gayle has a rocky cumulative strike between 150 -130s. Warner is steady around 120.

p1 <-batsmanCumulativeStrikeRate(kohli,"Kohli")
p2 <-batsmanCumulativeStrikeRate(warner, "DA Warner")
p3 <-batsmanCumulativeStrikeRate(akmal,"U Akmal")
p4 <-batsmanCumulativeStrikeRate(mccullum,"BB McCullum")
p5 <-batsmanCumulativeStrikeRate(emorgan,"EJG Morgan")
p6 <-batsmanCumulativeStrikeRate(gayle,"CH Gayle")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cSR-1

10. Batsman runs against opposition

#Kohli's best performances are against New Zealand and Sri Lanka
batsmanRunsAgainstOpposition(kohli,"Kohli")

runsOppn1-1

batsmanRunsAgainstOpposition(warner, "DA Warner")

runsOppn2-1

batsmanRunsAgainstOpposition(akmal,"U Akmal")

runsOppn3-1

batsmanRunsAgainstOpposition(mccullum,"BB McCullum")

runsOppn4-1

batsmanRunsAgainstOpposition(emorgan,"EJG Morgan")

runsOppn5-1

# Gayle's best performance is against India and South Africa
batsmanRunsAgainstOpposition(gayle,"CH Gayle")

runsOppn6-1

11. Runs at different venues

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

batsmanRunsVenue(kohli,"Kohli")

runsVenue1-1

batsmanRunsVenue(warner, "DA Warner")

runsVenue2-1

batsmanRunsVenue(akmal,"U Akmal")

runsVenue3-1

batsmanRunsVenue(mccullum,"BB McCullum")

runsVenue4-1

batsmanRunsVenue(emorgan,"EJG Morgan")

runsVenue5-1

batsmanRunsVenue(gayle,"CH Gayle")

runsVenue6-1

12. Predict number of runs to deliveries

The plots below use rpart classification tree to predict the number of deliveries required to score the runs in the leaf node. For e.g. Kohli takes <32 deliveries to score 22 runs and for higher number of deliveries scores around 66 runs. Devilliers needs <94 deliveries to score 84 runs and for greater deliveries scores around 109runs

par(mfrow=c(1,3))
par(mar=c(4,4,2,2))
batsmanRunsPredict(kohli,"Kohli")
batsmanRunsPredict(warner, "DA Warner")
batsmanRunsPredict(akmal,"U Akmal")

runsPredict1,runsVenue1-1

# BB McCullum needs >32 deliveries to score 69+ runs while Gayle needs >28 deliveries to score 67 runs
par(mfrow=c(1,3))
par(mar=c(4,4,2,2))
batsmanRunsPredict(mccullum,"BB McCullum")
batsmanRunsPredict(emorgan,"EJG Morgan")
batsmanRunsPredict(gayle,"CH Gayle")

runsPredict2,runsVenue1-1

B. Bowler functions

13. Get bowling details

The function below gets the overall team T20 bowling details based on the RData file available in T20 matches. This is currently also available in Github at [yorkrData] (https://github.com/tvganesh/yorkrData/tree/master/Twenty20/T20-matches). The T20 bowling details of the 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/Twenty20/T20-matches")
ind_bowling <- getTeamBowlingDetails("India",dir=".",save=TRUE)
dim(ind_bowling)
## [1] 872  12
aus_bowling <- getTeamBowlingDetails("Australia",dir=".",save=TRUE)
dim(aus_bowling)
## [1] 1364   12
eng_bowling <- getTeamBowlingDetails("England",dir=".",save=TRUE)
dim(eng_bowling)
## [1] 1183   12
sa_bowling <- getTeamBowlingDetails("South Africa",dir=".",save=TRUE)
dim(sa_bowling)
## [1] 995  12
pak_bowling <- getTeamBowlingDetails("Pakistan",dir=".",save=TRUE)
dim(pak_bowling)
## [1] 1186   12
nz_bowling <- getTeamBowlingDetails("New Zealand",dir=".",save=TRUE)
dim(nz_bowling)
## [1] 1295   12

14. Get bowling details of the individual 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

  1. Ravichander Ashwin (Ind)
  2. SR Watson (Aus)
  3. SCJ Broad (Eng)
  4. Saeed Ajmal (Pak)
  5. Dale Steyn (SA)
  6. NL McCullum (NZ)
ashwin <- getBowlerWicketDetails(team="India",name="Ashwin",dir=".")
watson <-  getBowlerWicketDetails(team="Australia",name="SR Watson",dir=".")
broad <-  getBowlerWicketDetails(team="England",name="SCJ Broad",dir=".")
ajmal <-  getBowlerWicketDetails(team="Pakistan",name="Saeed Ajmal",dir=".")
steyn <-  getBowlerWicketDetails(team="South Africa",name="Steyn",dir=".")
nmccullum <-  getBowlerWicketDetails(team="New Zealand",name="NL McCullum",dir=".")

15. Bowler Mean Economy Rate

Ashwin has a mean economy rate of 5.0 for 3 & 4 overs. Saeed Ajmal is more expensive

p1<-bowlerMeanEconomyRate(ashwin,"R Ashwin")
p2<-bowlerMeanEconomyRate(watson, "SR Watson")
p3<-bowlerMeanEconomyRate(broad, "SCJ Broad")
p4<-bowlerMeanEconomyRate(ajmal, "Saeed Ajmal")
p5<-bowlerMeanEconomyRate(steyn, "D Steyn")
p6<-bowlerMeanEconomyRate(nmccullum, "NL Mccullum")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

meanER-1

16. Bowler Mean Runs conceded

p1<-bowlerMeanRunsConceded(ashwin,"R Ashwin")
p2<-bowlerMeanRunsConceded(watson, "SR Watson")
p3<-bowlerMeanRunsConceded(broad, "SCJ Broad")
p4<-bowlerMeanRunsConceded(ajmal, "Saeed Ajmal")
p5<-bowlerMeanRunsConceded(steyn, "D Steyn")
p6<-bowlerMeanRunsConceded(nmccullum, "NL Mccullum")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

meanRunsConceded-1

17. Bowler Moving average

Aswin, SCJ Broad and Steyn have an improving performance in T20s. NL McCullum has a drop and Ajmal’s performance is on the decline

p1<-bowlerMovingAverage(ashwin,"R Ashwin")
p2<-bowlerMovingAverage(watson, "SR Watson")
p3<-bowlerMovingAverage(broad, "SCJ Broad")
p4<-bowlerMovingAverage(ajmal, "Saeed Ajmal")
p5<-bowlerMovingAverage(steyn, "D Steyn")
p6<-bowlerMovingAverage(nmccullum, "NL Mccullum")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

bowlerMA-1

17. Bowler cumulative average wickets

Interestingly Ajmal and NL McCullum have a cumulative average wickets of around 2.0. Steyn also has a cumulative average of 2.0+

p1<-bowlerCumulativeAvgWickets(ashwin,"R Ashwin")
p2<-bowlerCumulativeAvgWickets(watson, "SR Watson")
p3<-bowlerCumulativeAvgWickets(broad, "SCJ Broad")
p4<-bowlerCumulativeAvgWickets(ajmal, "Saeed Ajmal")
p5<-bowlerCumulativeAvgWickets(steyn, "D Steyn")
p6<-bowlerCumulativeAvgWickets(nmccullum, "NL Mccullum")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cumWkts-1

18. Bowler cumulative Economy Rate (ER)

Ajmal’s economy rate deteriorates from a excellent rate of 5.5, while Ashwin’s economy rate improves from a terrible rate of 9.0+.

p1<-bowlerCumulativeAvgEconRate(ashwin,"R Ashwin")
p2<-bowlerCumulativeAvgEconRate(watson, "SR Watson")
p3<-bowlerCumulativeAvgEconRate(broad, "SCJ Broad")
p4<-bowlerCumulativeAvgEconRate(ajmal, "Saeed Ajmal")
p5<-bowlerCumulativeAvgEconRate(steyn, "D Steyn")
p6<-bowlerCumulativeAvgEconRate(nmccullum, "NL Mccullum")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

cumER-1

19. Bowler wicket plot

The plot below gives the average wickets versus number of overs

p1<-bowlerWicketPlot(ashwin,"R Ashwin")
p2<-bowlerWicketPlot(watson, "SR Watson")
p3<-bowlerWicketPlot(broad, "SCJ Broad")
p4<-bowlerWicketPlot(ajmal, "Saeed Ajmal")
p5<-bowlerWicketPlot(steyn, "D Steyn")
p6<-bowlerWicketPlot(nmccullum, "NL Mccullum")
grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)

wktPlot-1

20. Bowler wicket against opposition

#Ashwin's best pertformance are against South Africa,Sri Lanka, Bangaldesh and Afghanistan
bowlerWicketsAgainstOpposition(ashwin,"R Ashwin")

wktsOppn1-1

#Watson's bets pertformance are against England, Ireland and New Zealand
bowlerWicketsAgainstOpposition(watson, "SR Watson")

wktsOppn2-1

bowlerWicketsAgainstOpposition(broad, "SCJ Broad")

wktsOppn3-1

#Ajmal's best performances are against Sri Lanka, New Zealand and South Africa
bowlerWicketsAgainstOpposition(ajmal, "Saeed Ajmal")

wktsOppn4-1

#Steyn has good performances against New Zealand, Sri Lanka, Pakistan, West Indies
bowlerWicketsAgainstOpposition(steyn, "D Steyn")

wktsOppn5-1

bowlerWicketsAgainstOpposition(nmccullum, "NL Mccullum")

wktsOppn6-1

21. Bowler wicket at cricket grounds

bowlerWicketsVenue(ashwin,"R Ashwin")

wktsAve1-1

bowlerWicketsVenue(watson, "SR Watson")

wktsAve2-1

bowlerWicketsVenue(broad, "SCJ Broad")

wktsAve3-1

bowlerWicketsVenue(ajmal, "Saeed Ajmal")

wktsAve4-1

bowlerWicketsVenue(steyn, "D Steyn")

wktsAve5-1

bowlerWicketsVenue(nmccullum, "NL Mccullum")

wktsAve6-1

22. Get Delivery wickets for bowlers

This function creates a dataframe of deliveries and the wickets taken

setwd("C:/software/cricket-package/york-test/yorkrData/Twenty20/T20-matches")
ashwin1 <- getDeliveryWickets(team="India",dir=".",name="Ashwin",save=FALSE)
watson1 <- getDeliveryWickets(team="Australia",dir=".",name="SR Watson",save=FALSE)
broad1 <- getDeliveryWickets(team="England",dir=".",name="SCJ Broad",save=FALSE)
ajmal1 <- getDeliveryWickets(team="Pakistan",dir=".",name="Saeed Ajmal",save=FALSE)
steyn1 <- getDeliveryWickets(team="South Africa",dir=".",name="Steyn",save=FALSE)
nmccullum1 <- getDeliveryWickets(team="New Zealand",dir=".",name="NL McCullum",save=FALSE)

23. Predict number of deliveries to wickets

#Ashwin takes <12 deliveries for a wicket while Watson takes around 9 deliveries
par(mfrow=c(1,2))
par(mar=c(4,4,2,2))
bowlerWktsPredict(ashwin1,"R Ashwin")
bowlerWktsPredict(watson1,"SR Watson")

wktsPred1-1

#Broad and Ajmal need around 8 deliveries for a wicket
par(mfrow=c(1,2))
par(mar=c(4,4,2,2))
bowlerWktsPredict(broad1,"SCJ Broad")
bowlerWktsPredict(ajmal1,"Saeed Ajmal")

wktsPred2-1

par(mfrow=c(1,2))
par(mar=c(4,4,2,2))
bowlerWktsPredict(steyn1,"D Steyn")
bowlerWktsPredict(nmccullum1,"NL Mccullum")

wktsPred3-1