# Addition
2-3
[1] -1
# Division
2/3
[1] 0.6666667
# Exponentiation
2^3 
[1] 8
#complex 
(2+5i)+ (3-1i)
[1] 5+4i
log(2)
[1] 0.6931472
# Exponentiation
2^3 
[1] 8
# Square root
sqrt(2)
[1] 1.414214
# Logarithms
log(2)
[1] 0.6931472

** #Question_1: Compute the log base 5 of 10 and the log of 10.**

log(10,5)  #Base5
[1] 1.430677
log(10,10) #Base10
[1] 1
log(100,4) #Base4
[1] 3.321928
#Batting Average=(No. of Hits)/(No. of At Bats)
#What is the batting average of a player that bats 129 hits in 412 at bats?

BA = 129/412
BA
[1] 0.3131068
#Alternative Solutions
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=142
Bat_Average<-N_Hits/At_Bats1
Batting_average=round(Bat_Average, digits = 3)
Batting_average
[1] 0.296
#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
OBJ_Adj=round(OBP,digits = 3)
OBJ_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

#AB=565,H=156,BB=65,HBP=3,SF=7
#Answers
OBP=(156+65+3)/(565+65+3+156+7)
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# Does 3 equals 8?
[1] FALSE
ls()
 [1] "At_Bats"         "At_Bats1"       
 [3] "BA"              "Bat_Average"    
 [5] "Batting_average" "Batting_Average"
 [7] "N_hits"          "N_Hits"         
 [9] "OBJ_Adj"         "OBP"            
[11] "OBP_Ad"         

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

Questions4 **

runs_per_9innings <-c(6,9,10,3,15)
runs_per_9innings
[1]  6  9 10  3 15
hits_per_9innings <-c(5,5,8,0,12)
hits_per_9innings
[1]  5  5  8  0 12
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
seq(2,13,by=3)
[1]  2  5  8 11
#adding vectors
pitches_by_innings+strikes_by_innings#+ operator
[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
#length
length(pitches_by_innings)
[1] 5
#find the minimum
min(pitches_by_innings)
[1] 10
#findaverage
mean(pitches_by_innings)
[1] 13.4
#pull parts of the vector
hits_per_9innings[1]
[1] 5
hits_per_9innings[length(hits_per_9innings)]
[1] 12
pitches_by_innings
[1] 12 15 10 20 10
pitches_by_innings[c(1:3)]
[1] 12 15 10
player_positions<-c("catcher","pitcher","infielder","outfielder")
player_positions
[1] "catcher"    "pitcher"    "infielder" 
[4] "outfielder"

Data Frames

data.frame(bonus = c(2, 3, 1),#in millions 
           active_roster = c("yes", "no", "yes"), 
           salary = c(1.5, 2.5, 1))#in millions 

using table

x <- c("Yes","No","No","Yes","Yes") 
table(x)
x
 No Yes 
  2   3 
y <- c("a","b","c","d","e") 
table(y)
y
a b c d e 
1 1 1 1 1 

Numerical measures of center and spread

ceo_salaries <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
ceo_salaries
 [1] 12.00  0.40  5.00  2.00 50.00  8.00  3.00  1.00  4.00  0.25
# the average
mean(ceo_salaries) 
[1] 8.565
var(ceo_salaries)
[1] 225.5145
sd(ceo_salaries)
[1] 15.01714
median(ceo_salaries)
[1] 3.5
fivenum(ceo_salaries)
[1]  0.25  1.00  3.50  8.00 50.00
# summary statistics
summary(ceo_salaries)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.250   1.250   3.500   8.565   7.250  50.000 
# Function to find the mode, i.e. most frequent value
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
#Question_8: Summarize the following survey with the `table()` command:
#What is your favorite day of the week to watch baseball? A total of 10 fans submitted this survey.
#Saturday, Saturday, Sunday, Monday, Saturday,Tuesday, Sunday, Friday, Friday, Monday
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 
#Question_9: What is the most frequent answer recorded in the survey? 
#Use the getMode function to compute results.
getMode(game_day)
[1] "Saturday"
LS0tCnRpdGxlOiAiR2V0dGluZyBTdGFydGVkIHdpdGggUiIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKYGBge3J9CiMgQWRkaXRpb24KMi0zCmBgYApgYGB7cn0KIyBEaXZpc2lvbgoyLzMKYGBgCmBgYHtyfQojIEV4cG9uZW50aWF0aW9uCjJeMyAKYGBgCgpgYGB7cn0KI2NvbXBsZXggCigyKzVpKSsgKDMtMWkpCmBgYApgYGB7cn0KbG9nKDIpCgpgYGAKYGBge3J9CiMgRXhwb25lbnRpYXRpb24KMl4zIApgYGAKYGBge3J9CiMgU3F1YXJlIHJvb3QKc3FydCgyKQpgYGAKYGBge3J9CiMgTG9nYXJpdGhtcwpsb2coMikKYGBgCioqICNRdWVzdGlvbl8xOiBDb21wdXRlIHRoZSBsb2cgYmFzZSA1IG9mIDEwIGFuZCB0aGUgbG9nIG9mIDEwLioqCmBgYHtyfQojQW5zd2Vycwpsb2coMTAsNSkgICNCYXNlNQpsb2coMTAsMTApIC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