Say I have a data table and I want to calculate a new variable based on several conditions of the old variables like this:
library(data.table)
test <- data.table(a = c(1,1,0), b = c(0,1,0), c = c(1,1,1))
test[a==1 & b==1 & c==1,test2:=1]
But I actually have many more conditions (all combinations of the different variables) which also have a different length. I draw those from a list such as:
conditions<-list(c("a","b","c"), c("b","c"))
and then I want to loop through that list and build a character vector like this (with which I want to do something before deleting it and going to the next element of the list):
mystring <- paste0(paste0(conditions[[1]], collapse = "==1 & "), "==1")
But how can I use "mystring" inside the data.table
? as.function() or get() or eval() don't seem to work. Something like:
test[mystring,test3:=1]
is what I'm looking for.