[R] Using with() to avoid $ ?

Bert Gunter bgunter.4567 at gmail.com
Sun Oct 23 21:43:46 CEST 2016


Yes, variables in the formula should be handled by nse with the data
argument. Got it -- thanks. But still ... can with() be used to handle
those and/or any other variables in foo that appear as arguments. I see no
problems in doing so, but ... ?

Bert

(But see inline below)

On Oct 23, 2016 7:24 PM, "Jeff Newmiller" <jdnewmil at dcn.davis.ca.us> wrote:
>
> No. And I don't know why you are conflating the treatment of variables in
the formula with treatment of variables passed as other arguments. It is
sort of like thinking the x symbols in foo$x[ x < 0 ] refer to the same
data.

In my query they explicitly do, though. Nevertheless your response was
apropos.

>
> foo$y ~ foo$x1 + foo$x2 + foo$x3 is not preferable, and given the
availability of a data argument such redundancy is unnecessary. NSE is
already in use for the formula. It is not (necessarily) in use for the
other arguments, so you just have to learn which arguments are being
handled with NSE by any particular function and which are not... good docs
would be the preferred avenue but recognizing the error message that arises
when you fail to specify foo$ for the non-formula arguments gets me by if
the docs are unclear.
>
> However, it is dangerous to apply NSE tricks recursively, so piling
"with" on top of the existing formula eval-with-data is only likely to
confuse the evaluation context even more.

This is what I'm not sure of. Can you give an example of when such
confusion would occur?


>
> --
> Sent from my phone. Please excuse my brevity.
>
> On October 23, 2016 9:18:17 AM PDT, Bert Gunter <bgunter.4567 at gmail.com>
wrote:
> >As has been noted oftimes on this list
> >f( y ~ x1 + x2 + x3 + ... , data = foo,  ...)
> >
> >is much preferable to
> >f( foo$y ~ foo$x1 + foo$x2 + foo$x3 + ...,  ...)
> >
> >(with no data argument), using nse = non-standard evaluation to set the
> >environment for formula evaluation. However, as queries here recently
> >demonstrate,  the formula variables (y, x1, x2, x3, ...) or other
> >variables
> >in foo are also sometimes needed as further arguments of f,  and these
> >have
> >to be explicitly and tediously given as foo$whatever or equivalent
> >indexing.
> >
> >So my question is, can/should with() be used instead in the form
> >with(foo, f( y ~ x1 + x2 + x3 + ... , data = foo,  ...))  with no
> >explicit
> >$ or indexing in ... variables?
> >
> >or even
> >with(foo, f( y ~ x1 + x2 + x3 + ... ,  ...))
> >
> >with no data argument for nse or indexing, though this seems to me
> >questionable in that it may affect the formula's  environment
> >differently.(??)
> >
> >Please correct any misstatements of fact in the above as well as
> >clarifying
> >anything else I seem confused about.
> >
> >Many thanks.
> >
> >Bert
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list