[R] Turning strings into expressions
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Nov 25 18:53:16 CET 2004
"Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be> writes:
> Hi Alexander,
>
> you could try:
>
> my.string <- "A==1 & B==2"
> (my.frame <- data.frame(A=sample(1:2, 20, TRUE), B=sample(1:2, 20,
> TRUE)))
> subset(my.frame, eval(parse(text=my.string)))
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
> substitute(subset(my.frame, sub), l)
subset(my.frame, A == 1 & B == 2)
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list