Learn R Programming

Sabermetrics (version 1.0)

slg: Slugging Percentage

Description

Function to calculate the slugging percentage (total hitting power) of a player/team

Usage

slg(TB, AB)

Arguments

TB
Total bases
AB
At bats

Value

  • Returns (TB/AB)

Details

Slugging percentage is a popular measure to determine the hitting power of an entity

References

http://en.wikipedia.org/wiki/Slugging_percentage

Examples

Run this code
## The slugging percentage (slg) function is currently defined as

function (TB, AB) 
{
    slugging <- (TB/AB)
    return(slugging)
  }
  
## Let's take 2014's MLB MVP, Mike Trout, and find his slugging percentage
## Stats for Mike Trout available on
## http://www.baseball-reference.com/players/t/troutmi01-bat.shtml
## For 2014, Trout had 602 AB, and 338 TB
## The formula for his slugging percentage using the slg function is below
## Output should be 0.5614618
slg(338, 602)

Run the code above in your browser using DataLab