Learn R Programming

⚠️There's a newer version (1.6.0) of this package.Take me there.

baseballr

baseballr 0.4

(latest version released 2019-03-18)

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

Installation

You can install baseballr from github with:

# install.packages("devtools")
devtools::install_github("BillPetti/baseballr")

Functionality

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 standings_on_date_bref() function. Just pass the year, month, day, and division you want:

library(baseballr)
standings_on_date_bref("2015-08-01", "NL East", from = FALSE)
#> $`NL East_up to_2015-08-01`
#>    Tm  W  L  W-L%   GB  RS  RA pythW-L%
#> 1 WSN 54 48 0.529   -- 422 391    0.535
#> 2 NYM 54 50 0.519  1.0 368 373    0.494
#> 3 ATL 46 58 0.442  9.0 379 449    0.423
#> 4 MIA 42 62 0.404 13.0 370 408    0.455
#> 5 PHI 41 64 0.390 14.5 386 511    0.374

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:

library(dplyr)
data <- daily_batter_bref("2015-08-01", "2015-10-03") %>%
  head()

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)
#> # A tibble: 30 x 5
#>    Team  Con_R Con_RA Con_R_Ptile Con_RA_Ptile
#>    <chr> <dbl>  <dbl>       <dbl>        <dbl>
#>  1 ARI    0.37   0.39          43           80
#>  2 ATL    0.38   0.36          65           25
#>  3 BAL    0.4    0.36          88           25
#>  4 BOS    0.37   0.39          43           80
#>  5 CHC    0.41   0.37          97           50
#>  6 CHW    0.38   0.36          65           25
#>  7 CIN    0.36   0.38          22           63
#>  8 CLE    0.38   0.42          65          100
#>  9 COL    0.38   0.38          65           63
#> 10 DET    0.39   0.35          82            3
#> # ... with 20 more rows

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 %>%
  filter(PA > 200) %>%
  woba_plus %>%
  arrange(desc(wOBA)) %>%
  select(Name, Team, season, PA, wOBA, wOBA_CON) %>%
  head()
#>               Name          Team season  PA  wOBA wOBA_CON
#> 1    Shin-Soo Choo         Texas   2015 260 0.430    0.495
#> 2 Francisco Lindor     Cleveland   2015 259 0.404    0.468
#> 3      Jose Altuve       Houston   2015 262 0.372    0.406
#> 4       Adam Eaton       Chicago   2015 262 0.367    0.436
#> 5    Manny Machado     Baltimore   2015 266 0.362    0.396
#> 6       Matt Duffy San Francisco   2015 264 0.312    0.338

You can also generate these wOBA-based stats, as well as FIP, for pitchers using the fip_plus() function:

daily_pitcher_bref("2015-04-05", "2015-04-30") %>% 
  fip_plus() %>% 
  select(season, Name, IP, ERA, SO, uBB, HBP, HR, FIP, wOBA_against, wOBA_CON_against) %>%
  arrange(desc(IP)) %>% 
  head(10)
#>    season            Name   IP  ERA SO uBB HBP HR  FIP wOBA_against
#> 1    2015    Johnny Cueto 37.0 1.95 38   4   2  3 2.62        0.210
#> 2    2015  Dallas Keuchel 37.0 0.73 22  11   0  0 2.84        0.169
#> 3    2015      Sonny Gray 36.1 1.98 25   6   1  1 2.69        0.218
#> 4    2015      Mike Leake 35.2 3.03 25   7   0  5 4.16        0.240
#> 5    2015 Felix Hernandez 34.2 1.82 36   6   3  1 2.20        0.225
#> 6    2015    Corey Kluber 34.0 4.24 36   5   2  2 2.40        0.295
#> 7    2015   Jake Odorizzi 33.2 2.41 26   8   1  0 2.38        0.213
#> 8    2015 Josh Collmenter 32.2 2.76 16   3   0  1 2.82        0.290
#> 9    2015   Bartolo Colon 32.2 3.31 25   1   0  4 3.29        0.280
#> 10   2015    Zack Greinke 32.2 1.93 27   7   1  2 3.01        0.240
#>    wOBA_CON_against
#> 1             0.276
#> 2             0.151
#> 3             0.239
#> 4             0.281
#> 5             0.272
#> 6             0.391
#> 7             0.228
#> 8             0.330
#> 9             0.357
#> 10            0.274

The edge_scrape() function allows the user to scrape PITCHf/x data from the GameDay application using Carson Sievert's pitchRx package and to calculate metrics associated with Edge%. The function returns a dataframe grouped by either pitchers or batters and the percentge of pitches in each of the various Edge zones.

Example (pitchers):

edge_scrape("2015-04-06", "2015-04-07", "pitcher") %>% 
  select(-6:-4, -13) %>% 
  head(10)
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_tormlb_nyamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_minmlb_detmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_colmlb_milmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_bosmlb_phimlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_balmlb_tbamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_nynmlb_wasmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_atlmlb_miamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_chamlb_kcamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_anamlb_seamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_pitmlb_cinmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_sdnmlb_lanmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_clemlb_houmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_texmlb_oakmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_sfnmlb_arimlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_atlmlb_miamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_balmlb_tbamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_colmlb_milmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_sfnmlb_arimlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_texmlb_oakmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_anamlb_seamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_sdnmlb_lanmlb_1/inning/inning_all.xml
#> # A tibble: 10 x 9
#>    pitcher_name   pitcher All_pitches Upper_Edge Lower_Edge Inside_Edge
#>    <chr>            <dbl>       <int>      <dbl>      <dbl>       <dbl>
#>  1 Bartolo Colon   112526          86      0.035      0.081       0.058
#>  2 LaTroy Hawkins  115629          12      0.083      0.333       0    
#>  3 Joe Nathan      150274           4      0          0           0    
#>  4 Buddy Carlyle   234194           9      0          0.222       0    
#>  5 Jason Grilli    276351          14      0          0           0.214
#>  6 Kevin Gregg     276514          17      0          0.059       0.118
#>  7 Joaquin Benoit  276542          19      0          0           0.158
#>  8 Ryan Vogelsong  285064          99      0.01       0.071       0.141
#>  9 Jeremy Affeldt  346793           5      0          0           0.4  
#> 10 Grant Balfour   346797          21      0.095      0           0    
#> # ... with 3 more variables: Outside_Edge <dbl>, Heart <dbl>,
#> #   Out_of_Zone <dbl>

Example (batters):

edge_scrape("2015-04-06", "2015-04-07", "batter") %>% 
  select(-6:-4, -13) %>% 
  head(10)
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_tormlb_nyamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_minmlb_detmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_colmlb_milmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_bosmlb_phimlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_balmlb_tbamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_nynmlb_wasmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_atlmlb_miamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_chamlb_kcamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_anamlb_seamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_pitmlb_cinmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_sdnmlb_lanmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_clemlb_houmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_texmlb_oakmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_06/gid_2015_04_06_sfnmlb_arimlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_atlmlb_miamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_balmlb_tbamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_colmlb_milmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_sfnmlb_arimlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_texmlb_oakmlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_anamlb_seamlb_1/inning/inning_all.xml 
#> http://gd2.mlb.com/components/game/mlb/year_2015/month_04/day_07/gid_2015_04_07_sdnmlb_lanmlb_1/inning/inning_all.xml
#> # A tibble: 10 x 9
#>    batter_name     batter All_pitches Upper_Edge Lower_Edge Inside_Edge
#>    <chr>            <dbl>       <int>      <dbl>      <dbl>       <dbl>
#>  1 Bartolo Colon   112526           7      0          0           0.429
#>  2 Torii Hunter    116338          19      0          0.158       0.105
#>  3 David Ortiz     120074          18      0          0           0.111
#>  4 Alex Rodriguez  121347          17      0          0           0.353
#>  5 Aramis Ramirez  133380          23      0          0.087       0.217
#>  6 Adrian Beltre   134181          26      0          0.038       0.154
#>  7 Carlos Beltran  136860          22      0.091      0           0.136
#>  8 Michael Cuddyer 150212          14      0          0.143       0.143
#>  9 Jimmy Rollins   276519          41      0.024      0.146       0.049
#> 10 Ryan Vogelsong  285064          10      0          0.1         0.3  
#> # ... with 3 more variables: Outside_Edge <dbl>, Heart <dbl>,
#> #   Out_of_Zone <dbl>

More functionality will be added soon. Please leave any suggestions or bugs in the Issues section.

Copy Link

Version

Install

install.packages('baseballr')

Monthly Downloads

1,407

Version

0.4

License

MIT + file LICENSE

Maintainer

Bill Petti

Last Published

January 16th, 2024

Functions in baseballr (0.4)

edge_scrape_split

Scrape and Calculating Edge Percentage for Splits
batter_boxscore

Retrieve batter boxscore data for a single game played
code_barrel

Code whether a batted ball is a "barrel" based on a Statcast dataset from baseballsavant.mlb.com
%>%

Pipe operator
daily_batter_bref

Scrape Batter Performance Data Over a Custom Time Frame
batter_game_logs_fg

Scrape Batter Game Logs from FanGraphs
daily_pitcher_bref

Scrape Pitcher Performance Data Over a Custom Time Frame
fg_park

Scrape Park Factors from FanGraphs.com
pitcher_game_logs_fg

Scrape Pitcher Game Logs from FanGraphs
fg_pitch_leaders

Scrape Pitcher Leaderboards from FanGraphs
fg_park_hand

Scrape Park Factors by Handedness from FanGraphs.com
fip_plus

Calculate FIP and related metrics for any set of data
process_statcast_payload

Process Baseball Savant CSV payload
%<>%

Compound_pipe
fg_bat_leaders

Scrape Batter Leaderboards from FanGraphs
edge_code

Edge Code
playerid_lookup

Look up Baseball Player IDs
linear_weights_savant

Generate linear weight values for events using Baseball Savant data
edge_frequency

Edge Percentage Frequency
master_ncaa_team_lu

A data set colleges and their athletic conferences and divisions
fg_guts

Scrape FanGraphs.com Guts!
run_expectancy_code

Generate run expectancy and related measures and variables from Baseball Savant data
standings_on_date_bref

Scrape MLB Standings on a Given Date
milb_batter_game_logs_fg

# Scrape MiLB game logs for batters from Fangraphs, combining 'standard' and 'advanced' tabs
statline_from_statcast

Create statlines that include count and rate metrics for players based on Statcast or PITCHf/x pitch-by-pitch data
milb_pitcher_game_logs_fg

# Scrape MiLB game logs for pitchers from Fangraphs, combining 'standard' and 'advanced' tabs
ggspraychart

Generate spray charts with ggplot2
viz_gb_on_period

Scrape MLB Standings on a Given Period and Visualize the Games Behind (GB) on any division or league the league
label_statcast_imputed_data

Label Statcast data as imputed
ncaa_scrape

Scrape NCAA baseball data (Division I, II, and III)
woba_plus

Calcuate wOBA and related metrics for any set of data
ncaa_season_id_lu

A data set of college baseball seasons
scrape_savant_leaderboards

run_expectancy_table

Generate run expectancy tables from Baseball Savant data
team_consistency

Calculate Team-level Consistency
playername_lookup

Look up Baseball Player Name
scrape_statcast_savant

school_id_lu

Lookup NCAA School IDs (Division I, II, and III)
team_results_bref

Scrape Team Results
edge_scrape

Edge Percentage Scrape