Hello,
I am trying to construct a logical expression dynamically, for use in the
subset() function. I am puzzled by problems with the code that follows
below.
Probably there's an easier way to select rows of a data frame according to
some dynamic criteria--and I'd love to hear about it--but I'd also like to
understand what I'm doing incorrectly in constructing the expression in my
code.
Thanks in advance,
tm
# These two expressions print the same, at least.
e1 = expression(name == 'a')
e2 = substitute(expression(cname == 'a'), list(cname=as.symbol('name')))
print (e1)
print (e2)
# But they do not evaluate the same!
# This works.
data = data.frame(name=c('a', 'b', 'c', 'a'), age=c(1,2,3,4))
d1 = subset(data, eval(e1))
print (d1)
# This gives the error:
# Error in subset.data.frame(data, eval(e2)) :
# 'subset' must evaluate to logical
d2 = subset(data, eval(e2))
print (d2)
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel