My aim is to code TRUE/FALSE evaluating a string generated from pasting three character cells. The following is what I have.
cond.var<-"a"
relation<-"!="
cond.value<-"1"
a<-1
as.numeric(eval(parse(text=paste(cond.var, relation, cond.value, sep=""))))
The above codes work when a number is stored in cond.value. The last line returns 0, which is correct. Yet, when NA is stored in cond.value (cond.value<-"NA"), the last line returns just NA, which is incorrect.
Any suggestion to fix this problem will be appreciated. Thanks.
Quite hackish but this would work without changing your setting
(using magrittr
-pipe to improve readability)
library(magrittr)
paste("`", relation, "`(", cond.var, ", ", cond.value, ")", sep="") %>%
sub(paste0(cond.var, ", NA)"), paste0("is.na(", cond.var, "), TRUE)"), .) %>%
parse(text=.) %>%
eval() %>%
as.numeric()