# Create the dataset
players <- data.frame(
PlayerID = c(1, 2, 3, 4, 5),
Hits = c(112, 124, 121, 106, 140),
At_Bats = c(400, 450, 380, 500, 402),
HR = c(25, 22, 8, 20, 11),
BB = c(50, 60, 19, 150, 55),
SO = c(60, 65, 67, 92, 70)
)
# Calculate On-Base Percentage (OBP)
players$OBP <- (players$Hits + players$BB) / (players$At_Bats + players$BB)
Please see the OBP below.
# View the table with OBP
players_summary <- players[, c("PlayerID", "Hits", "At_Bats", "BB", "OBP")]
players_summary$OBP <- round(players_summary$OBP, 3)
print(players_summary)
PlayerID Hits At_Bats BB OBP
1 1 112 400 50 0.360
2 2 124 450 60 0.361
3 3 121 380 19 0.351
4 4 106 500 150 0.394
5 5 140 402 55 0.427
The player with the highest OBP is Player ID 5. OBP= 0.427