My data.table dt0 is :
ANSWER1 ANSWER2 ANSWER3 ANSWER4 ANSWER5 ANSCOUNT SCORE100
21 37 16 14 11 200784 61
20 37 16 15 11 177165 60
ANSWERx are percentages. SCORE100 is computed as SCORE100 := (100*ANSWER1*ANSCOUNT + 75*ANSWER2*ANSCOUNT + 50*ANSWER3*ANSCOUNT + 25*ANSWER4*ANSCOUNT) / (ANSWER1*ANSCOUNT + ANSWER2*ANSCOUNT + ANSWER3*ANSCOUNT +ANSWER4*ANSCOUNT + ANSWER5*ANSCOUNT)]
In order to recompute SCORE100 for all lines in my data-set,
I need to automatically (in a loop) multiply all ANSWERx by ANSCOUNT in each row, then add all obtained multiplications and then divide by sum of all ANSCOUNT.
Can you help please,something along the lines below:
for (c in paste0("ANSWER",1:5)) {
dt0[, as.name(paste0(c,"_t")):= as.name(c)*"ANSCOUNT", , with=T]
}
Thank you.
Here's the error. How to understand it and how to do it right?
Error in `[.data.table`(dt0, , `:=`(as.name(paste0(c, "_t")), as.name(c) * :
LHS of := must be a symbol, or an atomic vector (column names or positions).