3 ==5
[1] FALSE
***Question_1: Compute the log base 5 of 10 and the log of 10.
log(10) # in of 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_1=(42)/212
Batting_Average1=round(BA_1,digits =3)
Batting_Average1
[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+56+3)/(565+65+7)
On_Base_Percentage2=round(OBP,digits = 3)
On_Base_Percentage2
[1] 0.338
Total_Bases <- 6 + 5
Total_Bases*3
[1] 33
ls
function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE,
pattern, sorted = TRUE)
{
if (!missing(name)) {
pos <- tryCatch(name, error = function(e) e)
if (inherits(pos, "error")) {
name <- substitute(name)
if (!is.character(name))
name <- deparse(name)
warning(gettextf("%s converted to character string",
sQuote(name)), domain = NA)
pos <- name
}
}
all.names <- .Internal(ls(envir, all.names, sorted))
if (!missing(pattern)) {
if ((ll <- length(grep("[", pattern, fixed = TRUE))) &&
ll != length(grep("]", pattern, fixed = TRUE))) {
if (pattern == "[") {
pattern <- "\\["
warning("replaced regular expression pattern '[' by '\\\\['")
}
else if (length(grep("[^\\\\]\\[<-", pattern))) {
pattern <- sub("\\[<-", "\\\\\\[<-", pattern)
warning("replaced '[<-' by '\\\\[<-' in regular expression pattern")
}
}
grep(pattern, all.names, value = TRUE)
}
else all.names
}
<bytecode: 0x58a201b7c7b0>
<environment: namespace:base>
pitches_by_innings <- c(12, 15, 10, 20, 10)
pitches_by_innings
[1] 12 15 10 20 10
wins_season<-c(85,65,78,75,55,55)
wins_season
[1] 85 65 78 75 55 55
strikes_by_innings <- c(9, 12, 6, 14, 9)
strikes_by_innings
#Question_4: Define two vectors,runs_per_9innings and hits_per_9innings, each with five elements.
uns_per_9innings<-c(25,12,6,1,7)
uns_per_8innings<-c(1,5,9,7,3)
uns_per_8innings
[1] 1 5 9 7 3
uns_per_9innings
[1] 25 12 6 1 7
pitches_by_innings <- c(12, 15, 10, 20, 10)
pitches_by_innings
[1] 12 15 10 20 10
total<-uns_per_8innings+uns_per_9innings
total
[1] 26 17 15 8 10
ls()
[1] "BA" "BA_1" "Batting_Average1"
[4] "OBP" "On_Base_Percentage" "On_Base_Percentage2"
[7] "pitches_by_innings" "strikes_by_innings" "total"
[10] "Total_Bases" "uns_per_8innings" "uns_per_9innings"
[13] "wins_season"
length(total)
[1] 5
min(total)
[1] 8
max(uns_per_8innings)
[1] 9
#Question_5: Get the first element of hits_per_9innings.
pitches_by_innings[1]
[1] 12
pitches_by_innings[c(2, 3, 4)]
[1] 15 10 20
#Question_6: Get the last element of hits_per_9innings.
hits_per_9innings<-c(41,15,12,7,15)
hits_per_9innings
[1] 41 15 12 7 15
tail(hits_per_9innings, 1)
[1] 15
hits_per_9innings[length(hits_per_9innings)]
[1] 15
player_positions <- c("catcher", "pitcher", "infielders", "outfielders")
player_positions
[1] "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
data.frame(uns_per_8innings,uns_per_9innings)
sample(1:10, size=5)
[1] 2 4 7 8 3
bar <- data.frame(var1 = LETTERS[1:10], var2 = 1:10)
# Check data frame
bar
x<-c("Yes","No","No","Yes","No", "Yes","No","Yes","Yes","Yes")
x
[1] "Yes" "No" "No" "Yes" "No" "Yes" "No" "Yes" "Yes" "Yes"
table(x)
x
No Yes
4 6
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
# the average
mean(sals)
[1] 8.565
var(sals)
[1] 225.5145
sd(sals)
[1] 15.01714
median(sals)
[1] 3.5
# Tukey's five number summary, usefull for boxplots
# five numbers: min, lower hinge, median, upper hinge, max
fivenum(sals)
[1] 0.25 1.00 3.50 8.00 50.00
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)))]
}
getMode(pitches_by_innings)
[1] 10
#Question_7: Find the most frequent value of hits_per_9innings.
getMode(wins_season)
[1] 55
getMode(total)
[1] 26
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"
getMode(game_day)
[1] "Saturday"