Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

battingStats

Calculate additional batting statistics


Description

The Batting does not contain batting statistics derived from those present in the data.frame. This function calculates batting average (BA), plate appearances (PA), total bases (TB), slugging percentage (SlugPct), on-base percentage (OBP), on-base percentage + slugging (OPS), and batting average on balls in play (BABIP) for each record in a Batting-like data.frame.

Usage

battingStats(data = Lahman::Batting, 
             idvars = c("playerID", "yearID", "stint", "teamID", "lgID"), 
             cbind = TRUE)

Arguments

data

input data, typically Batting

idvars

ID variables to include in the output data.frame

cbind

If TRUE, the calculated statistics are appended to the input data as additional columns

Details

Standard calculations, e.g., BA <- H/AB are problematic because of the presence of NAs and zeros. This function tries to deal with those problems.

Value

A data.frame with all the observations in data. If cbind==FALSE, only the idvars and the calculated variables are returned.

Author(s)

Michael Friendly, Dennis Murphy

See Also

Examples

bstats <- battingStats()
	str(bstats)
	bstats <- battingStats(cbind=FALSE)
	str(bstats)

Lahman

Sean 'Lahman' Baseball Database

v10.0-1
GPL
Authors
Michael Friendly [aut], Chris Dalzell [cre, aut], Martin Monkman [aut], Dennis Murphy [aut], Vanessa Foot [ctb], Justeena Zaki-Azat [ctb], Sean Lahman [cph]
Initial release
2022-04-07

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.