updated with example: I have a function as follows:
myfun <- function(DT, var){
for(i in 1:length(var)){
s = substitute(!(is.na(x) | is.nan(x)), list(x=as.symbol(eval(var[i]))))
DT = DT[eval(s)]
}
return(DT)
}
input:
> dt = data.table(id=c(1,2,3,4,5), x=c(1,2,NA,4,5), y=c(1,NA,3,4,NA))
> dt
id x y
1: 1 1 1
2: 2 2 NA
3: 3 NA 3
4: 4 4 4
5: 5 5 NA
runs:
> myfun(dt, var=c("x", "y"))
id x y
1: 1 1 1
2: 4 4 4
> myfun(dt, var=c("x"))
id x y
1: 1 1 1
2: 2 2 NA
3: 4 4 4
4: 5 5 NA
var is an character array of some variables in DT. The goal is to only obtain rows in DT which do not have any NA or NaN wrt to any of variables in var.
I do not WANT the for loop. I want to construct a query s with all the conditions and then evaluate that query for DT. for the first case I want:
s = !(is.na(x) | is.nan(x) | is.na(y) | is.nan(y))
and for the second case I want:
s = !(is.na(x) | is.nan(x))
How can I construct a dynamic query s and just run it once as an i/where query in the data table.
More generally how can I create dynamic expression based on input. Using expression(paste()) did not help me. Then I can use substitute.