My question relates to the creation of a variable which depends upon other columns within a data.table when none of the variable names are known in advance.
Below is
1. get/if
Try using get :
DT[, (Col4) := if (get(Col1) == "A") get(Col2) else get(Col3), by = 1:nrow(DT)]
2. get/join or try this approach:
setkeyv(DT, Col1)
DT[, (Col4):=get(Col3)]["A", (Col4):=get(Col2)]
3. .SDCols or this:
setkeyv(DT, Col1)
DT[, (Col4):=.SD, .SDcols = Col3]["A", (Col4):=.SD, .SDcols = Col2]
UPDATE: Added some additional approaches.