1+1
[1] 2
7-8
[1] -1
(2+5i) +(3-1i)
[1] 5+4i
4/2
[1] 2
6/12
[1] 0.5
2^2
[1] 4
2^5
[1] 32
sqrt(25)
[1] 5
sqrt(16)
[1] 4
sqrt(144)
[1] 12
log(2)#Natural log
[1] 0.6931472
log(10)
[1] 2.302585
log(2.72)
[1] 1.000632
log10(10)
[1] 1
Question 1:Compute the log base 5 of 10 and the log of 10.**
Answers:Question 1
log(10,5)#log of 10,base 5
[1] 1.430677
log(10,10)#log of 10, base 10
[1] 1
log(100,4)#Blog 100, base 4
[1] 3.321928
#Battting Average=(No. of Hits)//(No. of At Bats)
#What is the battling average of a player that bats 129 hits in 412 at bats?
BA=129/412
BA
[1] 0.3131068
#Alternative Solution
N_Hits=129
At_Bats=412
BA<-N_Hits/At_Bats
BA
[1] 0.3131068
Batting_Average=round(BA,digits=3)
Batting_Average
[1] 0.313
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.608
#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+165+7)
OBP_ad=round(OBP,digits =3)
OBP_ad
[1] 0.278
Often you will want to test whether something is less than, greater than or euqal to somehting.
3==9
[1] FALSE
2==3
[1] FALSE
1==1
[1] TRUE
3>=1
[1] TRUE
3>=9
[1] FALSE
7<=10
[1] TRUE
7<=6
[1] FALSE
3!=4
[1] TRUE
TRUE& FALSE
[1] FALSE
#Combination of statements
2<3|1==5 # 2<3 is True, 1==5 is False, True OR False is True
[1] TRUE
total_bases<-7+4
total_bases*4
[1] 44
Vectors
pitches_by_innings<-c(12,15,10,20,10)
pitches_by_innings
strikes_by_innins<-c(9,12,6,14,9)
strikes_by_innins
[1] 9 12 6 14 9
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
seq(1,10,by=3)
[1] 1 4 7 10
#adding vectors
pitches_by_innings+strikes_by_innins#+operator
[1] 21 27 16 34 19
#compare two vectors
pitches_by_innings
[1] 12 15 10 20 10
strikes_by_innins
[1] 9 12 6 14 9
pitches_by_innings==strikes_by_innins
[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
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" "outfielders"
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] 10.08125
var(ceo_salaries)
[1] 275.31
sd(ceo_salaries)
[1] 16.59247
median(ceo_salaries)
[1] 3.625
fivenum(ceo_salaries)
[1] 0.400 1.500 3.625 10.000 50.000
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] "yes"
strikes_by_innins
[1] 9 12 6 14 9
getMode(strikes_by_innins)
[1] "yes"
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