Skip to content
Prev 59848 / 398502 Next

Turning strings into expressions

"Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be> writes:
Hmm, considering the nonstandard evaluation that is going on inside
subset(), I think I'd rather try

 my.frame <- data.frame(A=sample(1:2, 20, TRUE), B=sample(1:2, 20,TRUE))
 my.string <- "A==1 & B==2"
 l <- as.list(parse(text=my.string))
 names(l)<-"sub"
 eval(substitute(subset(my.frame, sub), l))
   A B
18 1 2


(or perhaps l <- list(sub=parse(text=my.string)[[1]]) is less
cryptic).

Point being that this way you'll literally evaluate
subset(my.frame, A == 1 & B == 2)