Creating a Baseball Database with baseballDBR

June 12, 2017
By

[This article was first published on Data Science Riot!, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

My original motivation to write the baseballDBR package for R was to provide a quick and easy way to have access to Sean Lahman’s Baseball Database. The Lahman package has been around for several years, and is a great resource, however it lacks consistant updates. Also, the CRAN repository has limits on how large data packages can be, and the Lahman package is currently pushing that limit.

The answer was an “open-data” format that is maintained by the Chadwick Bureau’s Baseball Databank, which is based on Sean Lahman’s database, version 2015-01-24, but has additinal tables aggregated from Retrosheet data.

For further details, see the GitHub page for the baseballDBR package. In the meantime, we’ll spin through a few lines of code that will quickly get us up and running.

# Install the package from CRAN
install.packages(baseballDBR)

The following is based on the assumption we have an empty Postgres database called “lahman.” If you prefer another database, the following method should also work with MySQL and the RMySQL package.

library(baseballDBR)
library(RPostgreSQL)

# Load all tables into the Global Environment.
get_bbdb(AllTables = TRUE)

# Make a list of all data frames.
dbTables <- names(Filter(isTRUE, eapply(.GlobalEnv, is.data.frame)))

# Load data base drivers and load all data frames in a loop.
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host= "localhost", dbname= "lahman", user= "YOUR_USERNAME", password = "YOUR_PASSWORD")

for (i in 1:length(dbTables)) { 
    dbWriteTable(con, name =  dbTables[i], value = get0(dbTables[i]), overwrite = TRUE) 
}

# Disconnect from database.
dbDisconnect(con)
rm(con, drv)

To leave a comment for the author, please follow the link and comment on their blog: Data Science Riot!.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)