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

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jun 26 05:40:09 CEST 2010


On Fri, Jun 25, 2010 at 8:59 PM, Vadim Ogranovich
<vogranovich at jumptrading.com> wrote:
> 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))
>
>

If its ok to call it this way then it works without the eval:

do.call(subset, list(dat, subsetexp))



More information about the R-help mailing list