2+3 #Subtraction
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
log10(10)
## [1] 1
log10(10)
## [1] 1
log10(100)
## [1] 2
log(10, base=5)
## [1] 1.430677
#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_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)
On_Base_Percentage <- round(OBP, digits = 3)
On_Base_Percentage
## [1] 0.35
Total_Bases <- 6 + 5
Total_Bases*3
## [1] 33
pitches_by_innings <- c(12, 15, 10, 20, 10)
pitches_by_innings
## [1] 12 15 10 20 10
rep(2, 5)
## [1] 2 2 2 2 2
# consecutive numbers
1:5
## [1] 1 2 3 4 5
pitches_by_innings[c(2, 3, 4)]
## [1] 15 10 20
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
sample(1:10, size=5)
## [1] 4 2 9 6 10
bar <- data.frame(var1 = LETTERS[1:10], var2 = 1:10)
# Check data frame
bar
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
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 statistics
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_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")