Basic Calculations
2-3
## [1] -1
2/3
## [1] 0.6666667
2^3
## [1] 8
sqrt(2)
## [1] 1.414214
log(2)
## [1] 0.6931472
# Question 1
log(10, base=5)
## [1] 1.430677
log(10)
## [1] 2.302585
Batting Average
BA <- 29 / 112
BA
## [1] 0.2589286
Batting_Average <- round(BA, digits = 3)
Batting_Average
## [1] 0.259
# Question 2
BA_q2 <- 42 / 212
Batting_Average_q2 <- round(BA_q2, 3)
Batting_Average_q2
## [1] 0.198
On Base Percentage
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
OBP_q3 <- (156 + 65 + 3) / (565 + 65 + 3 + 7)
OBP_q3_rounded <- round(OBP_q3, 3)
OBP_q3_rounded
## [1] 0.35
Logical Comparisons
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
Assigning Values to Variables
Total_Bases <- 6 + 5
Total_Bases * 3
## [1] 33
ls()
## [1] "BA" "BA_q2" "Batting_Average"
## [4] "Batting_Average_q2" "OBP" "OBP_q3"
## [7] "OBP_q3_rounded" "On_Base_Percentage" "Total_Bases"
rm(Total_Bases)
Vectors
pitches_by_innings <- c(12, 15, 10, 20, 10)
strikes_by_innings <- c(9, 12, 6, 14, 9)
# Question 4
runs_per_9innings <- c(5, 4, 6, 3, 4)
hits_per_9innings <- c(8, 7, 10, 6, 7)
# Vector functions
rep(2, 5)
## [1] 2 2 2 2 2
rep(1, 4)
## [1] 1 1 1 1
1:5
## [1] 1 2 3 4 5
2:10
## [1] 2 3 4 5 6 7 8 9 10
seq(1, 10, by=2)
## [1] 1 3 5 7 9
seq(2, 13, by=3)
## [1] 2 5 8 11
# Vector math
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
# Access elements
pitches_by_innings[1]
## [1] 12
# Question 5
hits_per_9innings[1]
## [1] 8
# Question 6
hits_per_9innings[length(hits_per_9innings)]
## [1] 7
Strings and Data Frames
player_positions <- c("catcher", "pitcher", "infielders", "outfielders")
data.frame(bonus = c(2, 3, 1),
active_roster = c("yes", "no", "yes"),
salary = c(1.5, 2.5, 1))
## bonus active_roster salary
## 1 2 yes 1.5
## 2 3 no 2.5
## 3 1 yes 1.0
Random Sampling
bar <- data.frame(var1 = LETTERS[1:10], var2 = 1:10)
n <- 5
samplerows <- sample(1:nrow(bar), size=n)
barsample <- bar[samplerows, ]
barsample
## var1 var2
## 2 B 2
## 4 D 4
## 8 H 8
## 3 C 3
## 10 J 10
# One-liner
bar[sample(1:nrow(bar), n), ]
## var1 var2
## 3 C 3
## 1 A 1
## 6 F 6
## 8 H 8
## 4 D 4
Tables
x <- c("Yes","No","No","Yes","Yes")
table(x)
## x
## No Yes
## 2 3
Summary Stats
sals <- c(12, .4, 5, 2, 50, 8, 3, 1, 4, 0.25)
mean(sals)
## [1] 8.565
var(sals)
## [1] 225.5145
sd(sals)
## [1] 15.01714
median(sals)
## [1] 3.5
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
# Mode function
getMode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
# Question 7
getMode(hits_per_9innings)
## [1] 7
# Question 8
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
getMode(game_day)
## [1] "Saturday"