I believe that something close to the following (which uses the relatively new set()
function) will be the fastest:
DT <- data.table(id = c("A","B","C"), x1 = c(10,20,30), x2 = c(20,30,10)) total <- DT[ , x1 + x2] rr <- seq_len(nrow(DT)) for(j in 2:3) set(DT, rr, j, DT[[j]]/total) DT
FWIW, calls to set()
take the following form:
# set(x, i, j, value), where:
My suspicion about the relative speed of this, compared to other solutions, is based on this excerpt from the data.table of the NEW file , in the section on changes in version 1.8.0:
o New function set(DT,i,j,value) allows fast assignment to elements of DT. Similar to := but avoids the overhead of [.data.table, so is much faster inside a loop. Less flexible than :=, but as flexible as matrix subassignment. Similar in spirit to setnames(), setcolorder(), setkey() and setattr(); ie, assigns by reference with no copy at all. 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,1L] <- i)