## 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