O'Reilly logo

Baseball Hacks by Joseph Adler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Load Text Files into R

Load text files, such as Baseball Archive data or Retrosheet data, into R and save it as an R datafile for later access.

In the previous hack, I explain how to load data into R by connecting to a database. If you don’t want to install a database, you don’t have to. This hack explains how to load text files directly into R. Loading the data into R is pretty straightforward, but there are a few tricks. We’ll use the read.csv() command in R to load the data, which loads a comma-delimited text file into an R data frame. (For more information about functions and data frames in R, see “Analyze Baseball with R” [Hack #32] .)

R includes several functions for loading text files. Each is similar, but there are differences in the default separators for each one. You must specify a file location and then a field separator, a line separator, a decimal separator, column names, and several other options. They all return a data frame corresponding to the contents of the file, as shown in the following table:

Table 4-3. 

Function

Header

Field separator

Decimal separator

read.table

FALSE

None

Period

read.csv

TRUE

Comma

Period

read.csv2

TRUE

Semicolon

Comma

read.delim

TRUE

Tab

Period

read.delim2

TRUE

Tab

Comma

The Baseball Archive files include a header, are separated by commas, and use periods to indicate decimal points. I use the following read.csv() function. In this example, I assume that the files are in a directory called lahman52-csv. Make sure to use the correct location for the file if you run this code. (And remember, R uses forward slashes to separate directory names, even on Windows.)

	>batting <-read.csv(file="lahman52-csv/Batting.csv")
	>names(batting)
	[1]  "playerID" "yearID"   "stint"    "teamID"   "lgID"
	[6]  "G"        "AB"       "R"        "H"        "X2B"
	[11] "X3B"      "HR"       "RBI"      "SB"       "CS"
	[16] "BB"       "SO"       "IBB"      "HBP"      "SH"
	[21] "SF"       "GIDP"

Finally, I use the R save() function to save the data frame to an external file:

	save(batting, file="~/lahman52-csv/batting.RData")

This makes it easy to use this data any time you start R. Just use the load() command to load the files:

	load("~/lahman52-csv/batting.RData")

The Code

This code will load each file into a data frame in R and then save all of the files as an R datafile for later retrieval using the data() command. The code for loading Baseball Archive datafiles into R is included on the web site for this book (just download it and save yourself a lot of typing!). For reference, here is the complete code that I use to load the Baseball Archive files:

	teams <-read.csv(file="~/Desktop/book/data/lahman52/Teams.csv")
	master <-read.csv(file="~/Desktop/book/data/lahman52/Master.csv")
	batting <-read.csv(file="~/Desktop/book/data/lahman52/Batting.csv")
	pitching <-read.csv(file="~/Desktop/book/data/lahman52/Pitching.csv")
	fielding <-read.csv(file="~/Desktop/book/data/lahman52/Fielding.csv")
	allstars <-read.csv(file="~/Desktop/book/data/lahman52/allstar.csv")
	hof <- read.csv(file="~/Desktop/book/data/lahman52/halloffame.csv")
	managers <-read.csv(file="~/Desktop/book/data/lahman52/managers.csv")
	battingpost <-read.csv(file="~/Desktop/book/data/lahman52/battingpost.csv")
	pitchingpost <-read.csv(file="~/Desktop/book/data/lahman52/pitchingpost.csv")
	teamsfranchises <
	  -read.csv(file="~/Desktop/book/data/lahman52/teamsfranchises.csv")
	fieldingOF <-read.csv(file="~/Desktop/book/data/lahman52/fieldingOF.csv")
	managershalf <-read.csv(file="~/Desktop/book/data/lahman52/managershalf.csv")
	teamshalf <-read.csv(file="~/Desktop/book/data/lahman52/Teamshalf.csv")
	seriespost <-read.csv(file="~/Desktop/book/data/lahman52/seriespost.csv")
	awardsmanagers <-read.csv(file="~/Desktop/book/data/lahman52/awardsmanagers.csv")
	awardsplayers <-read.csv(file="~/Desktop/book/data/lahman52/awardsplayers.csv")
	save(allstars, awardsmanagers, awardsplayers, batting, battingpost,
	  fielding, fieldingOF, hof, managershalf, master, pitching,
	  pitchingpost, seriespost, teams, teamsfranchises, teamshalf,
	  file="~/Desktop/book/data/lahman52.RData")

Under file, make sure that you include the complete pathname of the location where you want to save the file. Many other hacks in this book will use this same data, so you’ll probably find it useful to load this data into R again (and not start with the text files each time!). To do that, use the following R command:

	> load("lahman52.RData")

(Of course, you should include the full pathname of the datafile.)

See Also

If you have MySQL installed, you might find it easier to load the data into R from the Baseball DataBank database. See “Access Databases Directly from Excel or R” [Hack #33] for instructions.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required