player_id <- 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)
players <- data.frame(PlayerID = player_id, Hits = hits, AtBats = at_bats,
HR = hr, BB = bb, SO = so)
players$OBP <- (players$Hits + players$BB) / (players$AtBats + players$BB)
players
## PlayerID Hits AtBats HR BB SO OBP
## 1 1 112 400 25 50 60 0.3600000
## 2 2 124 450 22 60 65 0.3607843
## 3 3 121 380 8 19 67 0.3508772
## 4 4 106 500 20 150 92 0.3938462
## 5 5 140 402 11 55 70 0.4266958
best_player <- players[which.max(players$OBP), ]
best_player
## PlayerID Hits AtBats HR BB SO OBP
## 5 5 140 402 11 55 70 0.4266958
The OBP for each player is 0.36, 0.361, 0.351, 0.394, 0.427.
The player with the highest OBP is Player 5 with an OBP of 0.427.