An update, several years later
This answer is old, and R has moved on. Tweaking
read.table to run a bit faster has precious little benefit. Your options are:
vroomfrom the tidyverse package
vroomfor importing data from csv/tab-delimited files directly into an R tibble. See Hector’s answer.
data.tablefor importing data from csv/tab-delimited files directly into R. See mnel’s answer.
readr(on CRAN from April 2015). This works much like
freadabove. The readme in the link explains the difference between the two functions (
readrcurrently claims to be “1.5-2x slower” than
iotoolsprovides a third option for quickly reading CSV files.
Trying to store as much data as you can in databases rather than flat files. (As well as being a better permanent storage medium, data is passed to and from R in a binary format, which is faster.)
sqldfpackage, as described in JD Long’s answer, imports data into a temporary SQLite database and then reads it into R. See also: the
RODBCpackage, and the reverse depends section of the
MonetDB.Rgives you a data type that pretends to be a data frame but is really a MonetDB underneath, increasing performance. Import data with its
dplyrallows you to work directly with data stored in several types of database.
Storing data in binary formats can also be useful for improving performance. Use
readRDS(see below), the
rhdf5packages for HDF5 format, or
The original answer
There are a couple of simple things to try, whether you use read.table or scan.
nrows=the number of records in your data (
Make sure that
comment.char=""to turn off interpretation of comments.
Explicitly define the classes of each column using
multi.line=FALSEmay also improve performance in scan.
If none of these thing work, then use one of the profiling packages to determine which lines are slowing things down. Perhaps you can write a cut down version of
read.table based on the results.
The other alternative is filtering your data before you read it into R.
Or, if the problem is that you have to read it in regularly, then use these methods to read the data in once, then save the data frame as a binary blob with
saveRDS, then next time you can retrieve it faster with