Consider the following dataset representing the performance of baseball players in a season. It includes the following variables: PlayerID, Hits, At-Bats, Home Runs (HR), Walks (BB), and Strikeouts (SO).
PlayerID Hits At-Bats HR BB SO
1 102 400 25 50 60
2 144 450 22 60 65
3 111 380 18 29 67
4 116 500 20 50 92
5 140 420 11 55 70
Compute the on-base percentage (OBP) for each player and select the player with the highest OBP.
To calculate OBP, you can use the following formula:
OBP = (Hits + Walks) / (At-Bats + Walks)
players <- data.frame(
PlayerID = c(1, 2, 3, 4, 5),
Hits = c(102, 144, 111, 116, 140),
AtBats = c(400, 450, 380, 500, 420),
Walks = c(50, 60, 29, 50, 55)
)
players$OBP <- (players$Hits + players$Walks) / (players$AtBats + players$Walks)
players
max_obp_player <- players[which.max(players$OBP),]
max_obp_player