I have a data.table and want to take a linear combination of the columns. How should I do it?
The setup
require(data.table)
set.seed(1)
DT <- data.table(A = rnorm(10),
B = rnorm(10),
C = rnorm(10),
D = rnorm(10),
coefA = rnorm(10),
coefB = rnorm(10),
coefC = rnorm(10),
coefD = rnorm(10))
I can do the following:
DT[, sum := A*coefA + B * coefB + C * coefC + D * coefD]
Is there a better way to solve this?