# 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"