Learn R Programming

Sabermetrics (version 2.0)

obp: On-Base Percentage

Description

The OBP function calculates how often a player gets on base.

Usage

obp(H, BB, HBP, AB, SF)

Arguments

H
Number of Hits
BB
Number of bases on balls (walks)
HBP
Number of hit by pitches
AB
Number of at bats
SF
Number of sacrifice flies

Value

Returns a numerical value equal to ((H+BB+HBP)/(AB+BB+SF+HBP))

References

https://en.wikipedia.org/wiki/On-base_percentage

See Also

ops

Examples

Run this code
## Let's calculate Mike Trout's OBP for the 2014 season
## He had 173 hits, 83 bases on balls, 10 hit by pitches, 602 at bats, and 10 sacrifice flies
## We should get 0.377 as our output
obp(173,83,10,602,10)

## The function is currently defined as
function (H, BB, HBP, AB, SF) 
{
    onbase <- ((H + BB + HBP)/(AB + BB + SF + HBP))
    return(onbase)
  }

Run the code above in your browser using DataLab