#Subtraction
2-3
[1] -1
#Addition
9+6
[1] 15
#Division
2/5
[1] 0.4
#Exponentiation
4^5
[1] 1024
#Square root
sqrt(2)
[1] 1.414214
#Logarithms
log(4) #Natural log
[1] 1.386294
Question_1: Compute the log base 5 of 10 and the log of 10.
#Answers: Question 1
log(10,5)# Log of 5, Base 10
log(10,10)# Log of 10, Base 10
log(100,4) # Log of 100, Base 4
Computing some 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
#Alternative Solution
N_Hits=29
At_Bats=112
BA<-N_Hits/At_Bats
BA
[1] 0.2589286
Batting_Average=round(BA,digits = 3)
Batting_Average
[1] 0.259
Question_2: What is the batting average of a player that bats 42 hits in 212 at bats?
#Answers
N_Hits=42
At_Bats1=212
Bat_Average<-N_Hits/At_Bats1
BattingAverage=round(Bat_Average,digits = 3)
BattingAverage
[1] 0.198
#On Base Percentage
#OBP=(H+BB+HBP)/(At Bats+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+84+5+6)
OBP
[1] 0.4278689
OBP_Adj=round(OBP,digits = 3)
OBP_Adj
[1] 0.428
Question_3:Compute the OBP for a player with the following general stats
#AB=565,H=156,BB=65,HBP=3,SF=7
OBP=(156+65+3)/(565+65+3+156+7)
OBP
[1] 0.281407
OBP_ad=round(OBP,digits=3)
OBP_ad
[1] 0.281
Often you will want to test whether something is less than, greater than or equal to something.
3==8
[1] FALSE
3!=8
[1] TRUE
3<=8
[1] TRUE
3>4
[1] FALSE
# Logical Disjunction (or)
FALSE|FALSE # False OR False
[1] FALSE
TRUE&FALSE
[1] FALSE
!FALSE
[1] TRUE
# Combination of statements
2<3|1==5 # 2<3 is True, 1==5 is False, True OR False is True
[1] TRUE
Assigning Values to Variables
#In R, you create a variable and assign it a value using <- as follow
Total_Bases<-6+5
Total_Bases*3
[1] 33
#To see the variables that are currently defined, use ls (as in “list”)
ls()
[1] "At_Bats" "At_Bats1"
[3] "BA" "Bat_Average"
[5] "Batting_Average" "BattingAverage"
[7] "N_Hits" "OBP"
[9] "OBP_ad" "OBP_adj"
[11] "OBP_Adj" "Total_Bases"
#To delete a variable, use rm (as in “remove”)
rm(Total_Bases)
Vectors
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
Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements.
rep(2,5)
[1] 2 2 2 2 2
rep(3,3)
[1] 3 3 3
1:6
[1] 1 2 3 4 5 6
2:7
[1] 2 3 4 5 6 7
#sequence from 1 to 10 with a step of 2
seq(1,10,by=2)
[1] 1 3 5 7 9
seq(2,13,by=3)
[1] 2 5 8 11
#add vectors
pitches_by_innings+strikes_by_innings
[1] 21 27 16 34 19
#compare two vectors
pitches_by_innings
[1] 12 15 10 20 10
strikes_by_innings
[1] 9 12 6 14 9
pitches_by_innings==strikes_by_innings
[1] FALSE FALSE FALSE FALSE FALSE
# find length of vector
length(pitches_by_innings)
[1] 5
# find minimum value in vector
min(pitches_by_innings)
[1] 10
mean(pitches_by_innings)
[1] 13.4
pitches_by_innings[1]
[1] 12
pitches_by_innings[length(pitches_by_innings)]
[1] 10
pitches_by_innings
[1] 12 15 10 20 10
pitches_by_innings[c(1:3)]
[1] 12 15 10
player_positions <- c("catcher","pitcher","infielders","outfielders")
player_positions
[1] "catcher" "pitcher" "infielders"
[4] "outfielders"
Data Frames
data.frame(bonus=c(2,3,1),active_roster=c("yes","no","yes"),
salary=c(1.5,2.5,1))
Using Tables
x<-c("Yes","No","No","Yes","Yes")
table(x)
x
No Yes
2 3
Numerical measures and center of spread
ceo_salaries<-c(12,.4,2,50,8,3,1,4,.25)
mean(ceo_salaries)
[1] 8.961111
var(ceo_salaries)
[1] 251.9386
sd(ceo_salaries)
[1] 15.87257
median(ceo_salaries)
[1] 3
fivenum(ceo_salaries)
[1] 0.25 1.00 3.00 8.00 50.00
#12,.4,2,15,8,3,1,4,.25
getMode<-function(x) {
ux<-unique(x)
ux[which.max(tabulate(match(x,ux)))]
}
pitches_by_innings
[1] 12 15 10 20 10
getMode(pitches_by_innings)
[1] 10
strikes_by_innings
[1] 9 12 6 14 9
getMode(strikes_by_innings)
[1] 9
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"