Frank Vega
CAP4936
R In Class activity 4
#Question_1: Compute the log base 5 of 10 and the log of 10.
log(10,base=5)
## [1] 1.430677
log(10)
## [1] 2.302585
#Question_2:What is the batting average of a player that bats 42 hits in 212 at bats?
BA1=(42)/(212)
Batting_Average=round(BA1,digits = 3)
Batting_Average
## [1] 0.198
#Question_3:Compute the OBP for a player with the following general stats:
#AB=565,H=156,BB=65,HBP=3,SF=7
#On Base Percentage
#OBP=(H+BB+HBP)/(At Bats+BB+HBP+SF)
OBP=(156+65+3)/(565+65+3+7)
OBP
## [1] 0.35
#Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements. 
runs_per_9innings <- c(12, 14, 21, 8, 1) 
runs_per_9innings
## [1] 12 14 21  8  1
hits_per_9innings <- c(15, 17, 8, 21, 13)
hits_per_9innings
## [1] 15 17  8 21 13
#Question_5: Get the first element of hits_per_9innings.
hits_per_9innings[1]
## [1] 15
#Question_6: Get the last element of hits_per_9innings.
hits_per_9innings[5]
## [1] 13
#Question_7: Find the most frequent value of hits_per_9innings.
getMode <- function(x) {
     ux <- unique(x)
     ux[which.max(tabulate(match(x, ux)))]
}

getMode(hits_per_9innings)
## [1] 15
#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")
game_day
##  [1] "Saturday" "Saturday" "Sunday"   "Monday"   "Saturday" "Tuesday" 
##  [7] "Sunday"   "Friday"   "Friday"   "Monday"
#Question_9: What is the most frequent answer recorded in the survey? Use the getMode function to compute results. 
getMode(game_day)
## [1] "Saturday"