4+3
## [1] 7
4-3
## [1] 1
4==3
## [1] FALSE
3^2
## [1] 9
sqrt(4)
## [1] 2
log (10) #ln, natural log, base e=2.72
## [1] 2.302585
3==5
## [1] FALSE
3==8
## [1] FALSE
3==3
## [1] TRUE
3==(2+1)
## [1] TRUE
4!=4
## [1] FALSE
2!=4
## [1] TRUE
log10 (10)
## [1] 1
log10 (100)
## [1] 2
log10 (1000)
## [1] 3
TRUE | FALSE
## [1] TRUE
TRUE & FALSE
## [1] FALSE
!FALSE
## [1] TRUE
!TRUE
## [1] FALSE
!FALSE & FALSE | TRUE
## [1] TRUE
!FALSE & TRUE | TRUE
## [1] TRUE
2>5|1==3
## [1] FALSE
11>7|4==3
## [1] TRUE

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

log (10, base = 5)
## [1] 1.430677
log10(10)
## [1] 1
#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

#Question_2:What is the batting average of a player that bats 42 hits in 212 at bats?

BA_1=(42)/212
Batting_Average1=round(BA_1,digits = 3)
Batting_Average1
## [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
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
## [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+7)
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
## [1] 0.35
Total_Bases<-136 + 214
Total_Bases
## [1] 350
ls()
## [1] "BA"                 "BA_1"               "Batting_Average"   
## [4] "Batting_Average1"   "OBP"                "On_Base_Percentage"
## [7] "Total_Bases"
rm(Total_Bases)
ls()
## [1] "BA"                 "BA_1"               "Batting_Average"   
## [4] "Batting_Average1"   "OBP"                "On_Base_Percentage"
Pitches_By_Innings <- c(12,15,10,20,10)
Pitches_By_Innings
## [1] 12 15 10 20 10
Wins_Season<-c(94,88,96,87,79)
Wins_Season
## [1] 94 88 96 87 79
strike_Innings<-c(5,6,9,7,14)
strike_Innings
## [1]  5  6  9  7 14
rep(2,5)
## [1] 2 2 2 2 2
1:6
## [1] 1 2 3 4 5 6
seq(3,10,3)
## [1] 3 6 9
strike_Innings
## [1]  5  6  9  7 14
Wins_Season
## [1] 94 88 96 87 79
Pitches_By_Innings
## [1] 12 15 10 20 10
strike_Innings+Pitches_By_Innings
## [1] 17 21 19 27 24
strike_Innings==Pitches_By_Innings
## [1] FALSE FALSE FALSE FALSE FALSE
length(Pitches_By_Innings)
## [1] 5
min(Pitches_By_Innings)
## [1] 10
max(Pitches_By_Innings)
## [1] 20
mean(Pitches_By_Innings)
## [1] 13.4
Pitches_By_Innings[3]
## [1] 10
Pitches_By_Innings[1]
## [1] 12
Pitches_By_Innings[5]
## [1] 10
Pitches_By_Innings[length(Pitches_By_Innings)]
## [1] 10
Pitches_By_Innings[c(2,3,4)]
## [1] 15 10 20
Players_Positions<-c("Catcher", "Pitchers", "Infielders","Outfielders")
Players_Positions
## [1] "Catcher"     "Pitchers"    "Infielders"  "Outfielders"
Soccer_Positions<-c("Goalkeepers", "Defenders", "Midfileders", "Forwards")
Soccer_Positions
## [1] "Goalkeepers" "Defenders"   "Midfileders" "Forwards"
data.frame(bonus = c(2, 3, 1),#in millions 
           active_roster = c("yes", "no", "yes"), 
           salary = c(1.5, 2.5, 1))#in millions
sample(1:9, size = 2)
## [1] 2 7
x<-c("yes", "no", "no", "no", "yes", "yes", "yes", "yes", "yes", "yes")
x
##  [1] "yes" "no"  "no"  "no"  "yes" "yes" "yes" "yes" "yes" "yes"
table(x)
## x
##  no yes 
##   3   7
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
mean(sals)
## [1] 8.565
var(sals)
## [1] 225.5145
sd(sals)
## [1] 15.01714
median(sals)
## [1] 3.5
# Tukeys 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(sals)
##    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)))]
 }
getMode(Pitches_By_Innings)
## [1] 10
getMode(Wins_Season)
## [1] 94
Wins_Season
## [1] 94 88 96 87 79
#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
getMode(game_day)
## [1] "Saturday"