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.

mean(cars$speed)
## [1] 15.4
mean(cars$dist)
## [1] 42.98
max(cars$dist)
## [1] 120
max(cars$speed)
## [1] 25
2^5
## [1] 32
log(2.72)
## [1] 1.000632
log(10, base=5)
## [1] 1.430677
log(10, base=2)
## [1] 3.321928
log(1000, base=10)
## [1] 3
log(10, base=5)
## [1] 1.430677
log(10, base=10)
## [1] 1

**Computing offensive metrics in Baseball

BA=(29)/(112)
BA
## [1] 0.2589286
Batting_Average=round(BA, digits=3)
Batting_Average
## [1] 0.259
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
3 == 8
## [1] FALSE
3 != 8
## [1] TRUE
3 <= 8
## [1] TRUE
3>4
## [1] FALSE
FALSE | FALSE
## [1] FALSE
TRUE & FALSE 
## [1] FALSE
! FALSE
## [1] TRUE
2 < 3 | 1 == 5
## [1] TRUE
Total_Bases <- 6 + 5
Total_Bases*3
## [1] 33
ls()
## [1] "BA"                 "Batting_Average"    "OBP"               
## [4] "On_Base_Percentage" "Total_Bases"

#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+156+65+3+7)
OBP
## [1] 0.281407
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

#Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements.

runs_per_9innings <- c(1, 2, 3, 2, 1)
runs_per_9innings
## [1] 1 2 3 2 1
hits_per_9innings <- c(3, 4, 3, 6, 5)
hits_per_9innings
## [1] 3 4 3 6 5
rep(2, 5)
## [1] 2 2 2 2 2
pitches_by_innings+strikes_by_innings
## [1] 21 27 16 34 19
pitches_by_innings == strikes_by_innings
## [1] FALSE FALSE FALSE FALSE FALSE
length(pitches_by_innings)
## [1] 5
min(pitches_by_innings)
## [1] 10
mean(pitches_by_innings)
## [1] 13.4

#Question_5: Get the first element of hits_per_9innings.

hits_per_9innings
## [1] 3 4 3 6 5
hits_per_9innings[1]
## [1] 3
pitches_by_innings[length(pitches_by_innings)]
## [1] 10

#Question_6: Get the last element of hits_per_9innings.

hits_per_9innings[length(hits_per_9innings)]
## [1] 5
player_positions <- c("catcher", "pitcher", "infielders", "outfielders")
data.frame(bonus = c(2, 3, 1),#in millions 
           active_roster = c("yes", "no", "yes"), 
           salary = c(1.5, 2.5, 1))#in millions 
x <- c("Yes","No","No","Yes","Yes") 
table(x)
## x
##  No Yes 
##   2   3
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
# the average
mean(sals) 
## [1] 8.565
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)))]
 }
# Most frequent value in pitches_by_innings
getMode(pitches_by_innings)
## [1] 10

#Question_7: Find the most frequent value of hits_per_9innings.

getMode(hits_per_9innings)
## [1] 3

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

#Question_9: What is the most frequent answer recorded in the survey? Use the getMode function to compute results.

getMode(game_day)
## [1] "Saturday"

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.