Skip to contents

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

Usage

fg_batter_leaders(
  age = "",
  pos = "all",
  stats = "bat",
  lg = "all",
  qual = "0",
  startseason = "2023",
  endseason = "2023",
  startdate = "",
  enddate = "",
  month = "0",
  hand = "",
  team = "0",
  pageitems = "10000",
  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
Batscharacter
xMLBAMIDinteger
PlayerNameRoutecharacter
PlayerNamecharacter
playeridinteger
Ageinteger
AgeRngcharacter
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
Defensenumeric
Offensenumeric
RARnumeric
WARnumeric
WAROldnumeric
Dollarsnumeric
BaseRunningnumeric
Spdnumeric
wRC_plusnumeric
wBsRnumeric
WPAnumeric
WPA_minusnumeric
WPA_plusnumeric
RE24numeric
REWnumeric
pLInumeric
PHinteger
WPA_LInumeric
Clutchnumeric
FBall_pctnumeric
FBvnumeric
SL_pctnumeric
SLvnumeric
CT_pctnumeric
CTvnumeric
CB_pctnumeric
CBvnumeric
CH_pctnumeric
CHvnumeric
SF_pctnumeric
SFvnumeric
XX_pctnumeric
wFBnumeric
wSLnumeric
wCTnumeric
wCBnumeric
wCHnumeric
wSFnumeric
wFB_Cnumeric
wSL_Cnumeric
wCT_Cnumeric
wCB_Cnumeric
wCH_Cnumeric
wSF_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
xwOBAnumeric
xAVGnumeric
xSLGnumeric
PPTVinteger
CPTVinteger
BPTVinteger
DSVinteger
DGVinteger
BTVinteger
rPPTVnumeric
rBPTVnumeric
EBVinteger
ESVinteger
rFTeamVnumeric
rBTeamVnumeric
rTVnumeric
pfx_FA_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_SC_pctnumeric
pfx_vFAnumeric
pfx_vFCnumeric
pfx_vFSnumeric
pfx_vFOnumeric
pfx_vSInumeric
pfx_vSLnumeric
pfx_vCUnumeric
pfx_vKCnumeric
pfx_vEPnumeric
pfx_vCHnumeric
pfx_vSCnumeric
pfx_FA-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_SC-Xnumeric
pfx_FA-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_SC-Znumeric
pfx_wFAnumeric
pfx_wFCnumeric
pfx_wFSnumeric
pfx_wFOnumeric
pfx_wSInumeric
pfx_wSLnumeric
pfx_wCUnumeric
pfx_wKCnumeric
pfx_wEPnumeric
pfx_wCHnumeric
pfx_wSCnumeric
pfx_wFA_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_wSC_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_CU_pctnumeric
pi_FA_pctnumeric
pi_FC_pctnumeric
pi_FS_pctnumeric
pi_SB_pctnumeric
pi_SI_pctnumeric
pi_SL_pctnumeric
pi_vCHnumeric
pi_vCUnumeric
pi_vFAnumeric
pi_vFCnumeric
pi_vFSnumeric
pi_vSBnumeric
pi_vSInumeric
pi_vSLnumeric
pi_CH-Xnumeric
pi_CU-Xnumeric
pi_FA-Xnumeric
pi_FC-Xnumeric
pi_FS-Xnumeric
pi_SB-Xnumeric
pi_SI-Xnumeric
pi_SL-Xnumeric
pi_CH-Znumeric
pi_CU-Znumeric
pi_FA-Znumeric
pi_FC-Znumeric
pi_FS-Znumeric
pi_SB-Znumeric
pi_SI-Znumeric
pi_SL-Znumeric
pi_wCHnumeric
pi_wCUnumeric
pi_wFAnumeric
pi_wFCnumeric
pi_wFSnumeric
pi_wSBnumeric
pi_wSInumeric
pi_wSLnumeric
pi_wCH_Cnumeric
pi_wCU_Cnumeric
pi_wFA_Cnumeric
pi_wFC_Cnumeric
pi_wFS_Cnumeric
pi_wSB_Cnumeric
pi_wSI_Cnumeric
pi_wSL_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
phLInumeric
pi_XX_pctnumeric
pi_vXXnumeric
pi_XX-Xnumeric
pi_XX-Znumeric
pi_wXXnumeric
pi_wXX_Cnumeric
rBTVnumeric
pi_CS_pctnumeric
pi_vCSnumeric
pi_CS-Xnumeric
pi_CS-Znumeric
pi_wCSnumeric
pi_wCS_Cnumeric
KN_pctnumeric
KNvnumeric
wKNnumeric
wKN_Cnumeric
pfx_KN_pctnumeric
pfx_vKNnumeric
pfx_KN-Xnumeric
pfx_KN-Znumeric
pfx_wKNnumeric
pfx_wKN_Cnumeric
pi_KN_pctnumeric
pi_vKNnumeric
pi_KN-Xnumeric
pi_KN-Znumeric
pi_wKNnumeric
pi_wKN_Cnumeric
rCPTVnumeric
CFramingnumeric
rDGVnumeric
rDSVnumeric

Examples

# \donttest{
  try(fg_batter_leaders(startseason = 2023, endseason = 2023))
#> ── MLB Player Batting Leaders data from FanGraphs.com ──────────────────
#>  Data updated: 2024-04-13 21:55:02 UTC
#> # A tibble: 1,457 × 348
#>    Season team_name Bats  xMLBAMID PlayerNameRoute   PlayerName playerid
#>     <int> <chr>     <chr>    <int> <chr>             <chr>         <int>
#>  1   2023 ATL       R       660670 Ronald Acuna Jr.  Ronald Ac…    18401
#>  2   2023 LAD       L       518692 Freddie Freeman   Freddie F…     5361
#>  3   2023 LAD       R       605141 Mookie Betts      Mookie Be…    13611
#>  4   2023 ATL       L       621566 Matt Olson        Matt Olson    14344
#>  5   2023 LAA       L       660271 Shohei Ohtani     Shohei Oh…    19755
#>  6   2023 TEX       L       608369 Corey Seager      Corey Sea…    13624
#>  7   2023 SDP       L       665742 Juan Soto         Juan Soto     20123
#>  8   2023 TEX       R       543760 Marcus Semien     Marcus Se…    12533
#>  9   2023 KCR       R       677951 Bobby Witt Jr.    Bobby Wit…    25764
#> 10   2023 MIL       R       661388 William Contreras William C…    20503
#> # ℹ 1,447 more rows
#> # ℹ 341 more variables: Age <int>, AgeRng <chr>, SeasonMin <int>,
#> #   SeasonMax <int>, G <int>, AB <int>, PA <int>, H <int>, `1B` <int>,
#> #   `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>, …
# }