DT = data.table(a=LETTERS[c(1,1:3)],b=4:7,key="a")
DT[,c:=8] # add a numeric column, 7 for all rows
DT[,d:=9L] # add an integer column, 8L for all rows
DT[,c:=NULL] # remove the c column
DT[2,d:=10L] # subassign by reference to column d
DT # DT changed by reference
DT[b>4,b:=d*2L] # subassign to b using d, where b>4
DT["A",b:=0L] # binary search for group "A" and set column b
DT[,newcol:=sum(v),by=group] # like fast transform() by group (not yet implemented)
# Speed example ...
m = matrix(1,nrow=100000,ncol=100)
DF = as.data.frame(m)
DT = as.data.table(m)
system.time(for (i in 1:1000) DF[i,1] <- i)
# 591 seconds
system.time(for (i in 1:1000) DT[i,V1:=i])
# 1.16 seconds ( 509 times faster )
Run the code above in your browser using DataLab