Creating a Baseball Database with baseballDBR

Kris Eberwein bio photo By Kris Eberwein Comment

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

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.


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

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

# 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.
rm(con, drv)

comments powered by Disqus