A package written for R focused on baseball analysis. Currently in development.
# You can install using the pacman package using the following code:
if (!requireNamespace('pacman', quietly = TRUE)){
install.packages('pacman')
}
pacman::p_load(dplyr)
baseballr
baseballr
is a package written for R focused on baseball analysis. It
includes functions for scraping various data from websites, such as
FanGraphs.com, Baseball-Reference.com, and baseballsavant.mlb.com. It also
includes functions for calculating metrics, such as wOBA, FIP, and
team-level consistency over custom time frames.
You can read more about some of the functions and how to use them at its
official site as well as this
Hardball Times
article.
You can install the CRAN version of
baseballr
with:
install.packages("baseballr")
You can install the released version of baseballr
from GitHub with:
# You can install using the pacman package using the following code:
if (!requireNamespace('pacman', quietly = TRUE)){
install.packages('pacman')
}
pacman::p_load_current_gh("BillPetti/baseballr")
# Alternatively, using the devtools package:
if (!requireNamespace('devtools', quietly = TRUE)){
install.packages('devtools')
}
devtools::install_github(repo = "BillPetti/baseballr")
For experimental functions in development, you can install the development branch:
# install.packages("devtools")
devtools::install_github("BillPetti/baseballr", ref = "development_branch")
The package consists of two main sets of functions: data acquisition and metric calculation.
For example, if you want to see the standings for a specific MLB division on a given date, you can use the bref_standings_on_date()
function. Just pass the year, month, day, and division you want:
library(baseballr)
library(dplyr)
bref_standings_on_date("2015-08-01", "NL East", from = FALSE)
Right now the function works as far as back as 1994, which is when both leagues split into three divisions.
You can also pull data for all hitters over a specific date range. Here are the results for all hitters from August 1st through October 3rd during the 2015 season:
data <- bref_daily_batter("2015-08-01", "2015-10-03")
data %>%
dplyr::glimpse()
In terms of metric calculation, the package allows the user to calculate the consistency of team scoring and run prevention for any year using team_consistency()
:
team_consistency(2015)
You can also calculate wOBA per plate appearance and wOBA on contact for any set of data over any date range, provided you have the data available.
Simply pass the proper data frame to woba_plus
:
data %>%
dplyr::filter(PA > 200) %>%
woba_plus %>%
dplyr::arrange(desc(wOBA)) %>%
dplyr::select(Name, Team, season, PA, wOBA, wOBA_CON) %>%
dplyr::glimpse()
You can also generate these wOBA-based stats, as well as FIP, for pitchers using the fip_plus()
function:
bref_daily_pitcher("2015-04-05", "2015-04-30") %>%
fip_plus() %>%
dplyr::select(season, Name, IP, ERA, SO, uBB, HBP, HR, FIP, wOBA_against, wOBA_CON_against) %>%
dplyr::arrange(dplyr::desc(IP)) %>%
head(10)
Please leave any suggestions or bugs in the Issues section.
Pull request are welcome, but I cannot guarantee that they will be accepted or accepted quickly. Please make all pull requests to the development branch for review.
To cite the baseballr
R package in publications, use:
BibTex Citation
@misc{petti_gilani_2021,
author = {Bill Petti and Saiem Gilani},
title = {baseballr: The SportsDataverse's R Package for Baseball Data.},
url = {https://billpetti.github.io/baseballr/},
year = {2021}
}