1

Often functions that work with data.frames have the ability to let the user provide a dataset, so that the user can use its columns in a straight forward way. E.g.:

lm(mpg~cyl+gear,data=mtcars)

Instead of using mtcars$cyl in the formula, we can simply use cyl. How can I implement such behavior in custom built functions?

joran
  • 169,992
  • 32
  • 429
  • 468
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • 3
    You can always look at the source of `lm` and see what's going on, or this wiki by Hadley may be of interest: https://github.com/hadley/devtools/wiki/Evaluation – Chase Jan 12 '12 at 15:26
  • very nice. thx Chase and James . Sometimes i just seem to forget about nice things... – Matt Bannert Jan 12 '12 at 15:41
  • Another good place to look for a worked example is the `subset.data.frame` function code. – IRTFM Jan 12 '12 at 17:28
  • Here was my answer to a recent ~ identical question: http://stackoverflow.com/questions/8484664/how-do-you-code-an-r-function-so-that-it-knows-to-look-in-data-for-the-varia/8484970#8484970 – Josh O'Brien Jan 12 '12 at 17:59
  • +1 on that other thread for you Josh, I think i am finally gettin started with other environments but global. Thx for the more elaborate answer though. – Matt Bannert Jan 12 '12 at 18:04

1 Answers1

1

There are several different techniques for this, described in Standard nonstandard valuation rules.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360