Skip to content
Prev 268008 / 398502 Next

enclosing with() in a function

Hi:

Here are a couple of ways; there may well be better ones.

# (1)  Use the get() function:
mean_on_element=function(data, elem_name) {
   with(data, mean(get(elem_name)))
 }
mean_on_element(data, 'x')

# (2) Lose 'with' and use subscripting instead:
mean_on_element=function(data, elem_name) {
   mean(data[[elem_name]])
 }
mean_on_element(data, 'x')

Since 'x' is quoted in the function call, you need to use code that
can convert the string 'x' to extracting the data object with name x.

HTH,
Dennis

On Mon, Aug 8, 2011 at 3:12 PM, thmsfuller066 at gmail.com
<thmsfuller066 at gmail.com> wrote: