I never really was concerned about what is the quickest way to read a CSV into R. THe reason for this is most of the data sets I deal with are very sample. So the time to read the file in is usually not very important. However, recently I had a project the required reading not just one .CSV into R, but rather a whole series of CSVs into R. using the standard read.csv() function thqt is built into R just to forever. So I switched to the data.table function fread. What a difference! I understand that data.table has been around for a while, but for the newer R user it is a really good package to know about once you get beyond toy datasets.
So I thought it would be really help to see just what the difference is between the two methods. FOr this example I an still using a relatively small data set. It is a little over five and a half million rows by six columns.
So for the read.csv function built in R
## Start timer
ptm<-proc.time()
test1<-read.csv("baby_data.csv")
## Stop timer and print time
ptm<-proc.time()-ptm
dim(test1)
## [1] 5674089 6
print(ptm)
## user system elapsed
## 33.427 0.495 33.945
for the fread function in the data.table
## Start timer
ptm<-proc.time()
require(data.table)
## Loading required package: data.table
test2<-fread("baby_data.csv")
##
Read 64.9% of 5674089 rows
Read 86.2% of 5674089 rows
Read 5674089 rows and 6 (of 6) columns from 0.187 GB file in 00:00:05
ptm<-proc.time()-ptm
print(ptm)
## user system elapsed
## 4.027 0.190 4.224
As you can see fread() is almost 10 times faster than read.csv to process this data set. That is pretty amazing. There is also a package called readr by Hadley Wickham that is a little slower than data.table but has some nice added features.