plot(cars)
cat("Max value of speedis :", max(cars$speed))
## Max value of speedis : 25
cat("Distance Average is:", mean(cars$dist))
## Distance Average is: 42.98
log(10, base=5)
## [1] 1.430677
log(10, base=10)
## [1] 1
Computing Offensive metrics in baseball
#Batting Average=(No. of Hits)/(No. of At Bats)
#What is the batting average of a player that bats 29 hits in 112 at bats?
BA=(29)/(112)
BA
## [1] 0.2589286
Batting_Average=round(BA,digits = 3)
Batting_Average
## [1] 0.259
#On Base Percentage
#OBP=(H+BB+HBP)/(At Bats+H+BB+HBP+SF)
#Let us compute the OBP for a player with the following general stats
#AB=515,H=172,BB=84,HBP=5,SF=6
OBP=(172+84+5)/(515+172+84+5+6)
OBP
## [1] 0.3337596
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
## [1] 0.334
3 == 8
## [1] FALSE
3 != 8# Is 3 different from 8?
## [1] TRUE
3>4
## [1] FALSE
FALSE | FALSE # False OR False
## [1] FALSE
TRUE & FALSE
## [1] FALSE
! FALSE
## [1] TRUE
2 < 3 | 1 == 5
## [1] TRUE
Total_Bases <- 6 + 5
Total_Bases * 3
## [1] 33
ls()
## [1] "BA" "Batting_Average" "OBP"
## [4] "On_Base_Percentage" "Total_Bases"
rm(Total_Bases)
pitches_by_innings <- c(12, 15, 10, 20, 10)
pitches_by_innings
## [1] 12 15 10 20 10
strikes_by_innings <- c(9, 12, 6, 14, 9)
strikes_by_innings
## [1] 9 12 6 14 9
runs_per_innings <- c(4,7,8,2,4)
runs_per_innings
## [1] 4 7 8 2 4
hits_per_9innings <- c(5, 8,1,9,3)
hits_per_9innings
## [1] 5 8 1 9 3
rep(2, 5)
## [1] 2 2 2 2 2
rep(1,4)
## [1] 1 1 1 1
1:5
## [1] 1 2 3 4 5
seq(1, 10, by=2)
## [1] 1 3 5 7 9
seq(2,13, by=3)
## [1] 2 5 8 11
pitches_by_innings + strikes_by_innings
## [1] 21 27 16 34 19
pitches_by_innings == strikes_by_innings
## [1] FALSE FALSE FALSE FALSE FALSE
length(pitches_by_innings)
## [1] 5
min(pitches_by_innings)
## [1] 10
mean(pitches_by_innings)
## [1] 13.4
pitches_by_innings[1]
## [1] 12
hits_per_9innings[1]
## [1] 5
pitches_by_innings[length(pitches_by_innings)]
## [1] 10
hits_per_9innings[length(hits_per_9innings)]
## [1] 3
pitches_by_innings[c(2, 3, 4)]
## [1] 15 10 20
player_positions <- c("catcher", "pitcher", "infielders", "outfielders")
data.frame(bonus = c(2, 3, 1),#in millions
active_roster = c("yes", "no", "yes"),
salary = c(1.5, 2.5, 1))#in millions
## bonus active_roster salary
## 1 2 yes 1.5
## 2 3 no 2.5
## 3 1 yes 1.0
sample(1:10, size=5)
## [1] 5 4 10 7 3
bar <- data.frame(var1 = LETTERS[1:10], var2 = 1:10)
bar
## var1 var2
## 1 A 1
## 2 B 2
## 3 C 3
## 4 D 4
## 5 E 5
## 6 F 6
## 7 G 7
## 8 H 8
## 9 I 9
## 10 J 10
n <- 5
samplerows <- sample(1:nrow(bar), size=n)
samplerows
## [1] 2 1 4 8 9
barsample <- bar[samplerows, ]
print(barsample)
## var1 var2
## 2 B 2
## 1 A 1
## 4 D 4
## 8 H 8
## 9 I 9
In a single line of code:
bar[sample(1:nrow(bar), n), ]
## var1 var2
## 4 D 4
## 10 J 10
## 3 C 3
## 8 H 8
## 2 B 2
x <- c("Yes","No","No","Yes","Yes")
table(x)
## x
## No Yes
## 2 3
mean
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
mean(sals)
## [1] 8.565
variance
# the variance
var(sals)
## [1] 225.5145
standard deviation
sd(sals)
## [1] 15.01714
median
median(sals)
## [1] 3.5
# Tukey's five number summary, usefull for boxplots
# five numbers: min, lower hinge, median, upper hinge, max
fivenum(sals)
## [1] 0.25 1.00 3.50 8.00 50.00
# summary statistics
summary(sals)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.250 1.250 3.500 8.565 7.250 50.000
getMode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
getMode(pitches_by_innings)
## [1] 10
getMode(hits_per_9innings)
## [1] 5
game_day<-c("Saturday", "Saturday", "Sunday", "Monday", "Saturday","Tuesday", "Sunday", "Friday", "Friday", "Monday")
table(game_day)
## game_day
## Friday Monday Saturday Sunday Tuesday
## 2 2 3 2 1
getMode(game_day)
## [1] "Saturday"