Skip to contents

This function allows you to scrape all leaderboard statistics (basic and advanced) from FanGraphs.com.

Usage

fg_team_batter(
  age = "",
  pos = "all",
  stats = "bat",
  lg = "all",
  qual = "y",
  startseason = "2023",
  endseason = "2023",
  startdate = "",
  enddate = "",
  month = "0",
  hand = "",
  team = "0,ts",
  pageitems = "1000",
  pagenum = "1",
  ind = "0",
  rost = "0",
  players = "",
  type = "8",
  postseason = "",
  sortdir = "default",
  sortstat = "WAR"
)

Arguments

age

(integer) Age of players

pos

(character) Position of players, defaults to "all". To exclude pitchers, use "np".

stats

(character) Statistic to return. Defaults to "bat".

lg

(character) League to return. Defaults to "all". Options are "al", "nl", or "all".

qual

(character) Whether you want only batters/pitchers that qualified in a given season, or the minimum number of plate appearances for inclusion. If you only want qualified hitters, use qual. If a minimum number of plate appearaces/innings pitched, use the number desired. Defaults to "y".

startseason

(character) Season for which you want to scrape the data.

endseason

(character) Last season for which you want data.

startdate

(character) Start date for which you want data.

enddate

(character) End date for which you want data.

month

(character) Month for which you want data.

hand

(character) Handedness of batter. Options are "L", "R", or "B". Empty string returns all.

team

(character) Teams for which you want data, comma separated.

pageitems

(character) Number of items per page.

pagenum

(character) Page number.

ind

(character) Whether or not to break the seasons out individual, or roll them up together. 1 = split seasons, 0 = aggregate seasons.

rost

(character) Whether or not to include players on the roster. 1 = include, 0 = exclude.

players

(character) Whether or not to include players on the roster. 1 = include only active roster players, 0 = exclude.

type

(character) Defaults to 8, which is the standard leaderboard. The values for the leaderboards appear to go to from type = 0 to 48+, which correspond to links on the leaderboard page.

postseason

(logical) Whether or not to include postseason data. TRUE = include postseason, FALSE = exclude postseason.

sortdir

(character) Sort direction. Options are "asc" or "desc" or "default".

sortstat

(character) Sort by stat. Default is "WAR".

Value

A data frame of batter data.

col_nametypes
Seasoninteger
team_namecharacter
SeasonMininteger
SeasonMaxinteger
Ginteger
ABinteger
PAinteger
Hinteger
1Binteger
2Binteger
3Binteger
HRinteger
Rinteger
RBIinteger
BBinteger
IBBinteger
SOinteger
HBPinteger
SFinteger
SHinteger
GDPinteger
SBinteger
CSinteger
AVGnumeric
GBinteger
FBinteger
LDinteger
IFFBinteger
Pitchesinteger
Ballsinteger
Strikesinteger
IFHinteger
BUinteger
BUHinteger
BB_pctnumeric
K_pctnumeric
BB_Knumeric
OBPnumeric
SLGnumeric
OPSnumeric
ISOnumeric
BABIPnumeric
GB_FBnumeric
LD_pctnumeric
GB_pctnumeric
FB_pctnumeric
IFFB_pctnumeric
HR_FBnumeric
IFH_pctnumeric
BUH_pctnumeric
TTO_pctnumeric
wOBAnumeric
wRAAnumeric
wRCnumeric
Battingnumeric
Fieldingnumeric
Replacementnumeric
Positionalnumeric
wLeaguenumeric
CFramingnumeric
Defensenumeric
Offensenumeric
RARnumeric
WARnumeric
WAROldnumeric
Dollarsnumeric
BaseRunningnumeric
Spdnumeric
wRC_plusnumeric
wBsRnumeric
WPAnumeric
WPA_minusnumeric
WPA_plusnumeric
RE24numeric
REWnumeric
pLInumeric
phLInumeric
PHinteger
WPA_LInumeric
Clutchnumeric
FBall_pctnumeric
FBvnumeric
SL_pctnumeric
SLvnumeric
CT_pctnumeric
CTvnumeric
CB_pctnumeric
CBvnumeric
CH_pctnumeric
CHvnumeric
SF_pctnumeric
SFvnumeric
KN_pctnumeric
KNvnumeric
XX_pctnumeric
wFBnumeric
wSLnumeric
wCTnumeric
wCBnumeric
wCHnumeric
wSFnumeric
wKNnumeric
wFB_Cnumeric
wSL_Cnumeric
wCT_Cnumeric
wCB_Cnumeric
wCH_Cnumeric
wSF_Cnumeric
wKN_Cnumeric
O-Swing_pctnumeric
Z-Swing_pctnumeric
Swing_pctnumeric
O-Contact_pctnumeric
Z-Contact_pctnumeric
Contact_pctnumeric
Zone_pctnumeric
F-Strike_pctnumeric
SwStr_pctnumeric
CStr_pctnumeric
C+SwStr_pctnumeric
Pullinteger
Centinteger
Oppointeger
Softinteger
Medinteger
Hardinteger
bipCountinteger
Pull_pctnumeric
Cent_pctnumeric
Oppo_pctnumeric
Soft_pctnumeric
Med_pctnumeric
Hard_pctnumeric
UBRnumeric
GDPRunsnumeric
AVG+numeric
BB_pct+numeric
K_pct+numeric
OBP+numeric
SLG+numeric
ISO+numeric
BABIP+numeric
LD_pct+numeric
GB_pct+numeric
FB_pct+numeric
HRFB_pct+numeric
Pull_pct+numeric
Cent_pct+numeric
Oppo_pct+numeric
Soft_pct+numeric
Med_pct+numeric
Hard_pct+numeric
rFTeamVinteger
rBTeamVinteger
rTVinteger
pfx_FA_pctnumeric
pfx_FT_pctnumeric
pfx_FC_pctnumeric
pfx_FS_pctnumeric
pfx_FO_pctnumeric
pfx_SI_pctnumeric
pfx_SL_pctnumeric
pfx_CU_pctnumeric
pfx_KC_pctnumeric
pfx_EP_pctnumeric
pfx_CH_pctnumeric
pfx_KN_pctnumeric
pfx_vFAnumeric
pfx_vFTnumeric
pfx_vFCnumeric
pfx_vFSnumeric
pfx_vFOnumeric
pfx_vSInumeric
pfx_vSLnumeric
pfx_vCUnumeric
pfx_vKCnumeric
pfx_vEPnumeric
pfx_vCHnumeric
pfx_vKNnumeric
pfx_FA-Xnumeric
pfx_FT-Xnumeric
pfx_FC-Xnumeric
pfx_FS-Xnumeric
pfx_FO-Xnumeric
pfx_SI-Xnumeric
pfx_SL-Xnumeric
pfx_CU-Xnumeric
pfx_KC-Xnumeric
pfx_EP-Xnumeric
pfx_CH-Xnumeric
pfx_KN-Xnumeric
pfx_FA-Znumeric
pfx_FT-Znumeric
pfx_FC-Znumeric
pfx_FS-Znumeric
pfx_FO-Znumeric
pfx_SI-Znumeric
pfx_SL-Znumeric
pfx_CU-Znumeric
pfx_KC-Znumeric
pfx_EP-Znumeric
pfx_CH-Znumeric
pfx_KN-Znumeric
pfx_wFAnumeric
pfx_wFTnumeric
pfx_wFCnumeric
pfx_wFSnumeric
pfx_wFOnumeric
pfx_wSInumeric
pfx_wSLnumeric
pfx_wCUnumeric
pfx_wKCnumeric
pfx_wEPnumeric
pfx_wCHnumeric
pfx_wKNnumeric
pfx_wFA_Cnumeric
pfx_wFT_Cnumeric
pfx_wFC_Cnumeric
pfx_wFS_Cnumeric
pfx_wFO_Cnumeric
pfx_wSI_Cnumeric
pfx_wSL_Cnumeric
pfx_wCU_Cnumeric
pfx_wKC_Cnumeric
pfx_wEP_Cnumeric
pfx_wCH_Cnumeric
pfx_wKN_Cnumeric
pfx_O-Swing_pctnumeric
pfx_Z-Swing_pctnumeric
pfx_Swing_pctnumeric
pfx_O-Contact_pctnumeric
pfx_Z-Contact_pctnumeric
pfx_Contact_pctnumeric
pfx_Zone_pctnumeric
pfx_Pacenumeric
pi_CH_pctnumeric
pi_CS_pctnumeric
pi_CU_pctnumeric
pi_FA_pctnumeric
pi_FC_pctnumeric
pi_FS_pctnumeric
pi_KN_pctnumeric
pi_SI_pctnumeric
pi_SL_pctnumeric
pi_XX_pctnumeric
pi_vCHnumeric
pi_vCSnumeric
pi_vCUnumeric
pi_vFAnumeric
pi_vFCnumeric
pi_vFSnumeric
pi_vKNnumeric
pi_vSInumeric
pi_vSLnumeric
pi_vXXnumeric
pi_CH-Xnumeric
pi_CS-Xnumeric
pi_CU-Xnumeric
pi_FA-Xnumeric
pi_FC-Xnumeric
pi_FS-Xnumeric
pi_KN-Xnumeric
pi_SI-Xnumeric
pi_SL-Xnumeric
pi_XX-Xnumeric
pi_CH-Znumeric
pi_CS-Znumeric
pi_CU-Znumeric
pi_FA-Znumeric
pi_FC-Znumeric
pi_FS-Znumeric
pi_KN-Znumeric
pi_SI-Znumeric
pi_SL-Znumeric
pi_XX-Znumeric
pi_wCHnumeric
pi_wCSnumeric
pi_wCUnumeric
pi_wFAnumeric
pi_wFCnumeric
pi_wFSnumeric
pi_wKNnumeric
pi_wSInumeric
pi_wSLnumeric
pi_wXXnumeric
pi_wCH_Cnumeric
pi_wCS_Cnumeric
pi_wCU_Cnumeric
pi_wFA_Cnumeric
pi_wFC_Cnumeric
pi_wFS_Cnumeric
pi_wKN_Cnumeric
pi_wSI_Cnumeric
pi_wSL_Cnumeric
pi_wXX_Cnumeric
pi_O-Swing_pctnumeric
pi_Z-Swing_pctnumeric
pi_Swing_pctnumeric
pi_O-Contact_pctnumeric
pi_Z-Contact_pctnumeric
pi_Contact_pctnumeric
pi_Zone_pctnumeric
pi_Pacenumeric
Eventsinteger
EVnumeric
LAnumeric
Barrelsinteger
Barrel_pctnumeric
maxEVnumeric
HardHitinteger
HardHit_pctnumeric
Qnumeric
TGinteger
TPAinteger
team_name_abbcharacter
teamidinteger
Posnumeric
pi_SB_pctnumeric
pi_vSBnumeric
pi_SB-Xnumeric
pi_SB-Znumeric
pi_wSBnumeric
pi_wSB_Cnumeric
pfx_SC_pctnumeric
pfx_vSCnumeric
pfx_SC-Xnumeric
pfx_SC-Znumeric
pfx_wSCnumeric
pfx_wSC_Cnumeric

Examples

# \donttest{
  try(fg_team_batter(startseason = 2015, endseason = 2015, qual = 200))
#> ── MLB Team Batting data from FanGraphs.com ───────── baseballr 1.6.0 ──
#>  Data updated: 2024-04-13 21:55:10 UTC
#> # A tibble: 30 × 329
#>    Season team_name SeasonMin SeasonMax     G    AB    PA     H  `1B`
#>     <int> <chr>         <int>     <int> <int> <int> <int> <int> <int>
#>  1   2015 TOR            2015      2015  2337  5509  6231  1480   923
#>  2   2015 SFG            2015      2015  2342  5565  6153  1486  1023
#>  3   2015 LAD            2015      2015  2379  5385  6090  1346   870
#>  4   2015 HOU            2015      2015  2333  5459  6073  1363   829
#>  5   2015 CHC            2015      2015  2406  5491  6200  1341   868
#>  6   2015 NYM            2015      2015  2325  5527  6145  1351   862
#>  7   2015 PIT            2015      2015  2431  5631  6285  1462  1003
#>  8   2015 STL            2015      2015  2406  5484  6139  1386   922
#>  9   2015 KCR            2015      2015  2263  5575  6116  1497  1016
#> 10   2015 TEX            2015      2015  2358  5511  6187  1419   936
#> # ℹ 20 more rows
#> # ℹ 320 more variables: `2B` <int>, `3B` <int>, HR <int>, R <int>,
#> #   RBI <int>, BB <int>, IBB <int>, SO <int>, HBP <int>, SF <int>,
#> #   SH <int>, GDP <int>, SB <int>, CS <int>, AVG <dbl>, GB <int>,
#> #   FB <int>, LD <int>, IFFB <int>, Pitches <int>, Balls <int>,
#> #   Strikes <int>, IFH <int>, BU <int>, BUH <int>, BB_pct <dbl>,
#> #   K_pct <dbl>, BB_K <dbl>, OBP <dbl>, SLG <dbl>, OPS <dbl>, …
# }