## The on-base percentage (obp) function is currently defined as
function (H, BB, HBP, AB, SF)
{
onbase <- ((H+BB+HBP)/(AB+BB+SF+HBP))
return(onbase)
}
## Let's take 2014's MLB MVP, Mike Trout, and find his on-base percentage
## Stats for Mike Trout available on
## http://www.baseball-reference.com/players/t/troutmi01-bat.shtml
## For 2014, Trout had 173 H, 83 BB, 10 HBP, 602 AB, 10 SF
## The formula for his on-base percentage using the obp function is below
## Output should be 0.377305
obp(173,83,10,602,10)
Run the code above in your browser using DataLab