[R] converting result of substitute to 'ordidnary' expression

From: Vadim Ogranovich <vogranovich_at_jumptrading.com>
Date: Fri, 25 Jun 2010 19:59:05 -0500

Dear R users,

As substitute() help page points out:

     Substituting and quoting often causes confusion when the argument
     is 'expression(...)'. The result is a call to the 'expression'
     constructor function and needs to be evaluated with 'eval' to give
     the actual expression object.

And indeed I am confused. Consider:

> dat <- data.frame(x=1:10, y=1:10)

> subsetexp <- substitute(a<x, list(a=5))

## this doesn't work
> subset(dat, subsetexp)

Error in subset.data.frame(dat, subsetexp) :   'subset' must evaluate to logical

## this does work (thanks to the help page), but one needs to remember to call eval
> subset(dat, eval(subsetexp))

Is there a way to create subsetexp that needs no eval inside the call to subset()?

I experimented with the following, but it didn't work:
> subsetexp <- eval(substitute(a<x, list(a=5)))
Error in eval(expr, envir, enclos) : object 'x' not found

Thank you very much for your help,

Note: This email is for the confidential use of the named addressee(s) only and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you are hereby notified that any review, dissemination or copying of this email is strictly prohibited, and to please notify the sender immediately and destroy this email and any attachments. Email transmission cannot be guaranteed to be secure or error-free. Jump Trading, therefore, does not make any guarantees as to the completeness or accuracy of this email or any attachments. This email is for informational purposes only and does not constitute a recommendation, offer, request or solicitation of any kind to buy, sell, subscribe, redeem or perform any type of transaction of a financial product.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 26 Jun 2010 - 01:03:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Jun 2010 - 04:20:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive