2/3
# Addition
2-3
[1] -1
# Division
2/3
[1] 0.6666667
# Exponentiation
2^3
[1] 8
# Square root
sqrt(2)
[1] 1.414214
# Logarithms
log(2)
[1] 0.6931472
#Question_1: Compute the log base 5 of 10 and the log of 10.
log(5,2)
[1] 2.321928
log(10)
[1] 2.302585
#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
Batting_Average=round(BA,digits = 3)
Batting_Average
[1] 0.259
#Question_2:What is the batting average of a player that bats 42 hits in 212 at bats?
BA2=(42)/(212)
BA2
[1] 0.1981132
Batting_Average2=round(BA2,digits = 3)
Batting_Average2
[1] 0.198
# OBP = (H + BB + HBP) / (AB + H + BB + HBP + SF)
# Example
OBP = (172 + 84 + 5) / (515 + 172 + 84 + 5 + 6)
OBP
[1] 0.3337596
On_Base_Percentage = round(OBP, digits = 3)
On_Base_Percentage
[1] 0.334
# Question 3: Compute the OBP for AB = 565, H = 156, BB = 65, HBP = 3, SF = 7
OBP2 = (156 + 65 + 3) / (565 + 156 + 65 + 3 + 7)
OBP2
[1] 0.281407
3 == 8 # Equals
[1] FALSE
3 != 8 # Not equals
[1] TRUE
3 <= 8 # Less than or equals
[1] TRUE
3 > 4 # Greater than
[1] FALSE
# Create vectors
pitches_by_innings = c(12, 15, 10, 20, 10)
strikes_by_innings = c(9, 12, 6, 14, 9)
# Question 4: Define runs_per_9innings and hits_per_9innings
runs_per_9innings = c(5, 4, 6, 7, 5)
hits_per_9innings = c(8, 7, 9, 10, 8)
# Question 5: Get the first element of hits_per_9innings
hits_per_9innings[1]
[1] 8
# Question 6: Get the last element of hits_per_9innings
hits_per_9innings[length(hits_per_9innings)]
[1] 8
# Create a data frame
data = data.frame(
bonus = c(2, 3, 1),
active_roster = c("yes", "no", "yes"),
salary = c(1.5, 2.5, 1)
)
# Random sampling
n = 2
sample_rows = sample(1:nrow(data), size = n)
sampled_data = data[sample_rows, ]
sampled_data
# Example: Survey of favorite baseball days
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 7: Find the most frequent value in game_day
getMode = function(x) {
ux = unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
getMode(game_day)
[1] "Saturday"