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)

mean(cars$speed)
## [1] 15.4
mean(cars$dist)
## [1] 42.98
max(cars$dist)
## [1] 120
max(cars$speed)
## [1] 25
4+1
## [1] 5
5-2
## [1] 3
2^2
## [1] 4
13/3
## [1] 4.333333
sqrt(25)
## [1] 5
2^5
## [1] 32
log(2.72) # Natural log of 2
## [1] 1.000632
log10(5)
## [1] 0.69897
log10(10)
## [1] 1
log10(100)
## [1] 2
# Here we are computing log base 10
log(10,base=5)
## [1] 1.430677
log(10,base=2)
## [1] 3.321928
log(1000,base=10)
## [1] 3

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

log(10,base=5)
## [1] 1.430677
log(10,base=10)
## [1] 1

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

BA=42/212
BA
## [1] 0.1981132
Batting_Average = round(BA,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

AB=565+156+65+3+7
BW=156+65+3
BV=(BW/AB)
OBP=round(BV,digits = 3)
OBP
## [1] 0.281
3==8
## [1] FALSE
3!=8
## [1] TRUE
3>5
## [1] FALSE
3<5
## [1] TRUE
FALSE|TRUE
## [1] TRUE
TRUE|FALSE
## [1] TRUE
FALSE & FALSE
## [1] FALSE
!FALSE & TRUE
## [1] TRUE
!TRUE & TRUE
## [1] FALSE
2<5 & 1 > 0
## [1] TRUE
Total_Bases <- 1*3
Total_Bases*12
## [1] 36
ls()
## [1] "AB"              "BA"              "Batting_Average" "BV"             
## [5] "BW"              "OBP"             "Total_Bases"
rm(Total_Bases)
ls()
## [1] "AB"              "BA"              "Batting_Average" "BV"             
## [5] "BW"              "OBP"
pitches_by_innings <- c(21,15,10,12)
pitches_by_innings
## [1] 21 15 10 12

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

runs_per9innings= c(1,1,2,4,15)
hits_per9innings= c(3,3,5,10,25)
runs_per9innings
## [1]  1  1  2  4 15
hits_per9innings
## [1]  3  3  5 10 25
rep(2,5)
## [1] 2 2 2 2 2
rep(10,4)
## [1] 10 10 10 10
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
10:5
## [1] 10  9  8  7  6  5
seq(1,20, by=2)
##  [1]  1  3  5  7  9 11 13 15 17 19
pitches_by_innings+runs_per9innings
## Warning in pitches_by_innings + runs_per9innings: longer object length is not a
## multiple of shorter object length
## [1] 22 16 12 16 36
pitches_by_innings!=runs_per9innings
## Warning in pitches_by_innings != runs_per9innings: longer object length is not
## a multiple of shorter object length
## [1] TRUE TRUE TRUE TRUE TRUE
length(runs_per9innings)
## [1] 5
min(runs_per9innings)
## [1] 1
max(pitches_by_innings)
## [1] 21
mean(runs_per9innings)
## [1] 4.6
runs_per9innings[3]
## [1] 2
runs_per9innings[length(runs_per9innings)]
## [1] 15

#Question_6: Get the last element of hits_per_9innings.

hits_per9innings[length(hits_per9innings)]
## [1] 25
runs_per9innings[c(2,4)]
## [1] 1 4
players = c("Ohtani","Skubal","Judge")
players
## [1] "Ohtani" "Skubal" "Judge"
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(12:57,size=6)
## [1] 38 47 33 29 30 43
bar = data.frame(var1 = LETTERS[1:10], var2 =1:10)
bar
n=5
samplerows= sample(1:nrow(bar),size=n)
samplerows
## [1] 6 9 5 7 2
barsample=bar[samplerows, ]
barsample
x=c("BMW","Honda","Nissan","BMW")
table(x)
## x
##    BMW  Honda Nissan 
##      2      1      1
sals=c(5,10,53,12,4.2,3.7)
mean(sals)
## [1] 14.65
var(sals)
## [1] 364.319
sd(sals)
## [1] 19.08714
median(sals)
## [1] 7.5
fivenum(sals)
## [1]  3.7  4.2  7.5 12.0 53.0
summary(sals)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3.70    4.40    7.50   14.65   11.50   53.00
getMode= function(x)
{
ux=unique(x)
ux[which.max(tabulate(match(x, ux)))]
}

getMode(x)
## [1] "BMW"

#Question_7: Find the most frequent value of hits_per_9innings.

getMode(hits_per9innings)
## [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")
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"