Skip to content

Instantly share code, notes, and snippets.

# ---------------- START: Who's the best allrounder in world cup cricket ----------------------
# read country and corresponding color info
country.color <- read.csv("country-colors.txt", header=T, sep=",")
# plot high runs/game and high wkts/game of players who've played at least N games in world cup cricket history
data <- world.cup.player.record[world.cup.player.record$ODI.played > 10 & !is.na(world.cup.player.record$Wickets.Per.Game) & !is.na(world.cup.player.record$Runs.Per.Game) & world.cup.player.record$High.Score.Probability > 0 & world.cup.player.record$High.Wicket.Probability > 0 & world.cup.player.record$Wickets.Per.Game > 0.8 & world.cup.player.record$Runs.Per.Game > 20,][,c("Player", "Country", "Runs.Per.Game", "Wickets.Per.Game", "High.Score.Probability", "High.Wicket.Probability", "Total.Batting.Runs.Scored", "Total.Wickets")]
# merge with country.color to get bubble color
data <- merge(data, country.color, by="Country")
# world.cup.player.record is a pre-computed data frame of player records.
> colnames(world.cup.player.record)
"Player", "Country", "ODI.played", "Total.Batting.Runs.Scored", "Total.Balls.Faced", "Total.Fours", "Total.Sixes", "Centuries", "Fifties", "Wins.When.Scored.Fifties", "Wins.When.Scored.Centuries", "High.Score.Probability", "Total.Wickets", "Total.Bowling.Runs.Given", Total.Overs.Bowled", "Total.Maidens", "Runs.Per.Game", "High.Wicket.Probability", "Economy" , "Five.Wicket.Hauls", "Three.Wicket.Hauls", "Wins.When.Three.Wicket.Hauls", "Wins.When.Five.Wicket.Hauls"
> country.color
country.color
Country Color
@prasoonsharma
prasoonsharma / gist:651807
Created October 28, 2010 17:08
R code for bar plot of Tendulkar and Ponting's batting performance
# DATA
tendulkar <- structure(list(A = c(1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010), B = c(215, 373, 78, 419, 640, 700, 58, 623, 1000, 647, 1088, 575, 1003, 1392, 153, 915, 444, 267, 776, 1063, 541, 1003), C = c(35.83, 37.3, 19.5, 38.09, 71.11, 63.64, 14.5, 41.53, 58.82, 71.89, 57.26, 57.5, 55.72, 53.54, 17, 61, 44.4, 22.25, 48.5, 42.52, 60.11, 77.15)), .Names = c("Year", "Total.Runs.In.Year", "Runs.Per.Match.In.Year"), class = "data.frame", row.names = c(NA, -22L))
ponting <- structure(list(A = c(1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010), B = c(NA, NA, NA, NA, NA, NA, 167, 163, 497, 382, 883, 318, 772, 1064, 1503, 697, 1444, 1333, 192, 1182, 853, 551), C = c(NA, NA, NA, NA, NA, NA, 83.5, 20.38, 45.18, 27.29, 51.94, 45.43, 32.17, 66.5, 83.5, 36.68, 55.54, 74.06, 32, 47.28, 37.09, 42.38)), .Names = c("Year", "Total.Runs.In.Year", "Ru
@prasoonsharma
prasoonsharma / gist:716246
Created November 26, 2010 03:36
Using variables in R
# VARIABLES IN R
# Assignment
x <- 7
y <- 8
# Using other variables
z <- sqrt(x*x + y*y)
# Printing
@prasoonsharma
prasoonsharma / gist:716248
Created November 26, 2010 03:39
Using R as a calculator
# USE R AS A CALCULATOR
# Comments (like this one) are indicated with a # in R
# Addition
1 + 2
# Subtraction
3.2 - 1.8
@prasoonsharma
prasoonsharma / gist:716254
Created November 26, 2010 03:43
Atomic data types in R
# ATOMIC DATA TYPES IN R
# Character
first.name <- "Kirk"
# Integer
age <- 25
# Numeric
hourly_wage <- 27.25
@prasoonsharma
prasoonsharma / gist:716272
Created November 26, 2010 04:07
Data structures in R
# DATA STRUCTURES IN R
# Vector: a sequence of elements of same atomicity
# Vectors can be formed by using the concatenate function c()
alphabets <- c("a", "b", "c")
alphabets
numbers <- c(1, 2, 3)
numbers
@prasoonsharma
prasoonsharma / gist:717808
Created November 27, 2010 11:15
Vector operations in R
# VECTOR OPERATIONS IN R
# Create vectors
age <- c(28, 19, 29, 81, 60, 55, 44, 23)
year <- 1990:1995 # notice the use of : operator for sequence function (seq)
five.ones <- rep(1, 5) # checkout the use of repeat function
# a vector of odd numbers between 1 and 100
odd.numbers <- seq(1, 100, by=2)
?seq
@prasoonsharma
prasoonsharma / gist:717849
Created November 27, 2010 12:21
Getting help on R
# LOCAL HELP
# R offers great help articles with the install
# Just type ? and the function if you know the function name
?sqrt # Pay special attention to function definition and take a look at the examples
# Type ?? and your search keyword to search a keyword in R help documentation
??maximum
@prasoonsharma
prasoonsharma / gist:717887
Created November 27, 2010 13:23
Curriculum for Introduction to R
CURRICULUM FOR INTRODUCTION TO R (DRAFT)
Day 1:
a) Install R
b) Use R as a calculator
Day 2:
a) Getting help
b) Use variables in R
c) Understand atomic data types in R