I'm wondering if there is a faster way to multiply a set of columns by values in a vector (essentially applying a regression formula). I'm seeing this post but it requires converting a data.table to a matrix which is inefficient.
I also see this old post that includes a for loop but the loop I tested is much, much slower than what I have here and given the age of the post I'm wondering if there are other ways.
Any other suggestions?
set.seed(100)
N = 2e7L
DT1 = data.table(id = sample(letters[1:10], N, TRUE),
a = sample(0:3, N, TRUE),
b = sample(0:3, N, TRUE),
c = sample(0:3, N, TRUE),
d = sample(0:3, N, TRUE))
v <- c(0.2232332, 0.332424, 0.3322525250, 0.32342323432)
# 528.4188 ms
microbenchmark({
DT1[,blah:=a*v[1] + b*v[2] + c*v[3] + d*v[4]]
}, times = 1, unit = "ms")