I am trying to do a simple thing by dividing 40 columns of a data table by their average value. I cannot provide the actual data (not all columns are numeric, but I have> 8M rows), but here is an example:
library(data.table) dt <- data.table(matrix(sample(1:100,4000,T),ncol=40)) colmeans <- colMeans(dt)
Next, I thought what I would do:
for (col in names(colmeans)) dt[,col:=dt[,col]/colmeans[col]]
But this returns an error since dt[,col] requires that column names not be specified. Using as.name(col) does not cut it. Now,
res <- t(t(dt[,1:40,with=F]/colmeans))
contains an accelerated result, but I cannot insert it back into the data table.
dt[,1:40] <- res
does not work, and dt[,1:40:=res, with=F] .
The following works, but I find it pretty ugly:
for (i in seq_along(colmeans)) dt[,i:=dt[,i,with=F]/colmeans[i],with=F]
Of course, I could also recreate the new data table by calling data.table() on res and other non-numeric columns that my data.table has, but isnโt it something more efficient?