Comments (2)
The source data does not return game_id, but it does return game_pk, which can be used to discern between two games of a double header.
from baseballr.
Thanks. I've written a function that adds a PITCHf/x gameday_link
to the Statcast data frame returned by your scraping function. Feel free to use and improve:
#' Create PITCHf/x gameday_link for Statcast
#'
#' This function allows you to add a PITCHf/x gameday_link to Statcast data.
#' @param x a data frame with Statcast variables: game_date, home_team, away_team, sv_id, game_pk
#' @keywords MLB, sabermetrics, Statcast, PITCHf/x, gameday_link
#' @importFrom plyr
#' @export
#' @examples
#' \dontrun{
#' stopifnot(require(baseballr))
#' x <- scrape_statcast_savant_batter_all("2016-05-07", "2016-05-07")
#' x <- sc_add_gameday_link(x)
#' }
sc_add_gameday_link <- function(x) {
# create lookup to convert team abbreviations to those used by PITCHf/x
abb <- data.frame(
px.name = c("ari","atl","bal","bos","chn","cin","cle","col","cha","det",
"hou","kca","ana","lan","mia","mil","min","nyn","nya","oak",
"phi","pit","sdn","sea","sfn","sln","tba","tex","tor","was"),
sc.name = c("ARI","ATL","BAL","BOS","CHC","CIN","CLE","COL","CWS","DET",
"HOU","KC","LAA","LAD","MIA","MIL","MIN","NYM","NYY","OAK",
"PHI","PIT","SD","SEA","SF","STL","TB","TEX","TOR","WSH"),
stringsAsFactors = F)
# sort statcast dataframe by sv_id
x <- x[with(x, order(sv_id)), ]
x <- merge(x, abb, by.x = "home_team", by.y = "sc.name")
names(x)[names(x) == "px.name"] <- "px.home"
x <- merge(x, abb, by.x = "away_team", by.y = "sc.name")
names(x)[names(x) == "px.name"] <- "px.away"
# group by game_date and home_team, create link
x <- plyr::ddply(x,
c("game_date", "home_team"),
transform,
gameday_link = paste0("gid_", gsub("-", "_", game_date), "_",
px.away, "mlb_",
px.home, "mlb_",
ifelse(game_pk == game_pk[1], 1, 2))
, stringsAsFactors = FALSE)
# remove temp variables in dataframe
x <- within(x, rm(px.home, px.away))
# return original Statast data plus gameday_link
return(x)
}
from baseballr.
Related Issues (20)
- Fangraphs Batter leaders
- Umpire Data from load_umpire_ids() Ends with games 2022-09-07 HOT 2
- bref_standings_on_date no longer functioning HOT 1
- ncaa_park_factor error HOT 1
- Are there any examples of using sit_code successfully?
- ind argument returns error in the fg_pitcher_leaders function HOT 6
- fangraphs defense HOT 2
- add Chadwick helper functions HOT 1
- fg_team_pitcher function does not work HOT 5
- playerid_lookup returning a 404 HOT 3
- Function arent working HOT 3
- baseballr::fg_pitcher_leaders no longer works HOT 1
- fg_batter_game_logs no longer working HOT 6
- package pulled from CRAN HOT 11
- Statcast-Baseballr Potential discrepancy in Barrels values between Savantbaseball web SEARCH and Baseballr scraped data
- fg_pitcher_leaders no longer works
- bref_daily_pitcher() and bref_daily_batter() not working
- Pre-2002 data isn't able to be pulled in with fg_batter_leaders function
- Fangraph batter splits using 'month'
- fg_batter_leaders splits using 'month' argument
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from baseballr.