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
Using ifelse and get:
ifelse
get
DT[, (Col4) := ifelse (get(Col1) == "A",get(Col2) , get(Col3))]
Or using substitute to create the expression like this :
substitute
expr <- substitute(a4 := ifelse (a1 == "A",a2 , a3), list(a1=as.name(Col1), a2=as.name(Col2), a3=as.name(Col3), a4=as.name(Col4))) DT[, eval(expr)]