This is an old revision of the document!


Data Table

Data table is a very useful package in R which allows to facilitate and to improve the efficiency of certain operations in R. Data tables are just like data frames. You can even create them from data frames.

Introduction to Data table (PDF)

install.packages('data.table')
library(data.table)
library(reshape2)
library(data.table)
library(plyr)
mydf=data.frame(a=rep(LETTERS,each=1e6),b=rnorm(26*1e6))
mydt=data.table(mydf)
setkey(mydt,a)
mydt['F']
# Returns all rows with column a (the key) equal to F 
mydt[,mean(b),by=a]
# Gives the mean value of column b for each letter in column a. 
# Compare
system.time(t1<-mydt[,mean(b),by=a])
# 0.314 secs
# With
system.time(t2<-tapply(mydf$b,mydf$a,mean))
# 7.239 secs
meltdf=melt(mydf)
system.time(t3<-dcast(meltdf,a~variable,mean))
# 4.453 secs
system.time(t4<-ddply(mydf,.(a),summarize,mean(b)))
# 2.288 secs
ti1<-proc.time()
t5<-data.frame(letter=unique(mydf$a),mean=rep(0,26))
for (i in t5$letter ){
  t5[t5$letter==i,2]=mean(mydf[mydf$b==i,2])
}
eltime<-proc.time()-ti1