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.
log(10)
## [1] 2.302585
log10(10)
## [1] 1
log10(100)
## [1] 2
log10(1000)
## [1] 3
#log base 5 of 10
log(10, base = 5)
## [1] 1.430677
#log of 10
log(10)
## [1] 2.302585
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
#Question_2:What is the batting average of a player that bats 42 hits in 212 at bats?
BA=(42)/(212)
BA=round(BA, digits=3)
BA
## [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)
OBP
## [1] 0.35
3==5
## [1] FALSE
3==8
## [1] FALSE
3==3
## [1] TRUE
3==(2+1)
## [1] TRUE
4!=4
## [1] FALSE
TRUE | FALSE
## [1] TRUE
TRUE & FALSE
## [1] FALSE
!FALSE
## [1] TRUE
!FALSE & FALSE | TRUE
## [1] TRUE
!FALSE & !TRUE | TRUE
## [1] TRUE
2 > 5 | 1==3
## [1] FALSE
11>7 | 4==3
## [1] TRUE
Total_Bases<-136+214
Total_Bases
## [1] 350
ls()
## [1] "BA" "OBP" "On_Base_Percentage"
## [4] "Total_Bases"
rm(Total_Bases)
ls()
## [1] "BA" "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,1)
strike_innings
## [1] 5 6 9 7 1
Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements.
runs_per_9innings<-c(5, 6, 8, 9, 7)
hits_per_9innings<-c(4, 8, 6, 2, 7)
runs_per_9innings
## [1] 5 6 8 9 7
hits_per_9innings
## [1] 4 8 6 2 7
1:6
## [1] 1 2 3 4 5 6
seq(3,10)
## [1] 3 4 5 6 7 8 9 10
strike_innings
## [1] 5 6 9 7 1
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 11
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
Question_5: Get the first element of hits_per_9innings. Question_6: Get the last element of hits_per_9innings.
hits_per_9innings[1]
## [1] 4
hits_per_9innings[length(hits_per_9innings)]
## [1] 7
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] 7 1
x <- c("Yes","No","No","Yes","Yes", "No", "No","No","Yes","Yes")
x
## [1] "Yes" "No" "No" "Yes" "Yes" "No" "No" "No" "Yes" "Yes"
table(x)
## x
## No Yes
## 5 5
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
sals
## [1] 12.00 0.40 5.00 2.00 50.00 8.00 3.00 1.00 4.00 0.25
var(sals)
## [1] 225.5145
sd(sals)
## [1] 15.01714
median(sals)
## [1] 3.5
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)))]
}
pitches_by_innings
## [1] 12 15 10 20 10
getMode(pitches_by_innings)
## [1] 10
wins_Season
## [1] 94 88 96 87 79
getMode(wins_Season)
## [1] 94
Question_7: Find the most frequent value of hits_per_9innings.
getMode(hits_per_9innings)
## [1] 4
#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"