This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
15.4
## [1] 15.4
42.98
## [1] 42.98
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
4+1
## [1] 5
5-2
## [1] 3
2/3
## [1] 0.6666667
2^3
## [1] 8
2-3
## [1] -1
sqrt(2)
## [1] 1.414214
log(2.72)
## [1] 1.000632
log10(10)
## [1] 1
log(10,base=2)
## [1] 3.321928
#Module_1 Activity_1 #Question_1 compute Log base 5 of 10 and log of 10
print("Log base 5 of 10 is:")
## [1] "Log base 5 of 10 is:"
log(10,base=5)
## [1] 1.430677
print("log base 10 is:")
## [1] "log base 10 is:"
log(10)
## [1] 2.302585
**Computing some offensive metrics in Baseball
BA=(29/112)
BA
## [1] 0.2589286
Battling_Average=round(BA,digit =3 )
Battling_Average
## [1] 0.259
#Question_2 What is the batting average of a player that bats 42 hits in 212 at bats?
#Batting Average=(No. of Hits)/(No. of At Bats)
BA=(42/212)
BA
## [1] 0.1981132
#OBP=(H+BB+HBP)/(At Bats+H+BB+HBP+SF)
#AB=515,H=172,BB=84,HBP=5,SF=6
OBP=(172+84+5)/(515+172+84+5+6)
OBP
## [1] 0.3337596
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
## [1] 0.334
#Question_3:Compute the OBP for a player with the following general stats
#AB=565,H=156,BB=65,HBP=3,SF=7
#OBP=(H+BB+HBP)/(At Bats+H+BB+HBP+SF)
OBP=(156+65+3)/(565+156+65+3+7)
OBP
## [1] 0.281407
On_Base_Percentage=round(OBP,digits = 3)
On_Base_Percentage
## [1] 0.281
#Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements.
Run_per_9innings <- c(14, 18, 15, 22, 16)
Run_per_9innings
## [1] 14 18 15 22 16
Hits_per_9innings <- c(22, 14, 20, 10, 12)
Hits_per_9innings
## [1] 22 14 20 10 12
#Question_5: Get the first element of hits_per_9innings.
Hits_per_9innings[1]
## [1] 22
#Question_6: Get the last element of hits_per_9innings.
Hits_per_9innings[5]
## [1] 12
#Question_7: Find the most frequent value of hits_per_9innings.
# Function to find the mode, i.e. most frequent value
getMode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
getMode(Hits_per_9innings)
## [1] 22
#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<-c("Saturday", "Saturday", "Sunday", "Monday", "Saturday","Tuesday", "Sunday", "Friday", "Friday", "Monday")
print("Favorite day of the week to watch baseball:")
## [1] "Favorite day of the week to watch baseball:"
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.
# Function to find the mode, i.e. most frequent value
getMode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
game_day<-c("Saturday", "Saturday", "Sunday", "Monday", "Saturday","Tuesday", "Sunday", "Friday", "Friday", "Monday")
print("The most frequent answer recorded in the survey is: ")
## [1] "The most frequent answer recorded in the survey is: "
getMode(game_day)
## [1] "Saturday"