This is almost 2x faster for me than your manual version:
Reduce("+", lapply(names(DT), function(x) DT[[x]] * cf[x]))
benchmark(manual = DT[, list(cf['A']*A+cf['B']*B+cf['C']*C+cf['D']*D)],
reduce = Reduce('+', lapply(names(DT), function(x) DT[[x]] * cf[x])))
# test replications elapsed relative user.self sys.self user.child sys.child
#1 manual 100 1.43 1.744 1.08 0.36 NA NA
#2 reduce 100 0.82 1.000 0.58 0.24 NA NA
And to iterate over just mycols
, replace names(DT)
with mycols
in lapply
.