Skip to content
Prev 268013 / 398502 Next

enclosing with() in a function

On Aug 9, 2011, at 00:29 , Dennis Murphy wrote:

            
I suspect this goes belly-up if there's a column data$elem_name, though.

Given than with() is essentially evalq() which in turn is eval(quote(...),...), the obvious way to achieve the desired effect would be to omit quoting the argument and do

eval(substitute(mean(elem_name)), data) 

or, to avoid unexpected variable capture:

mean_on_element <- function(data, elem_name) 
   eval(substitute(mean(elem_name)), data, parent.frame()) 

mean_on_element(airquality, Day)

Or rather: this allows variable capture of the same kind that with() allows:
[1] 0.575
[1] 0.575