title: Pass a data.frame column name to a function
body: <p>I'm trying to write a function to accept a data.frame (<code>x</code>) and a <code>column</code> from it. The function performs some calculations on x and later returns another data.frame. I'm stuck on the best-practices method to pass the column name to the function.</p>
  
  <p>The two minimal examples <code>fun1</code> and <code>fun2</code> below produce the desired result, being able to perform operations on <code>x$column</code>, using <code>max()</code> as an example. However, both rely on the seemingly (at least to me) inelegant </p>
  
  <ol>
  <li>call to <code>substitute()</code> and possibly <code>eval()</code> </li>
  <li>the need to pass the column name as a character vector. </li>
  </ol>
  
  <p></p>
  
  <pre><code>fun1 &lt;- function(x, column){
    do.call("max", list(substitute(x[a], list(a = column))))
  }
  
  fun2 &lt;- function(x, column){
    max(eval((substitute(x[a], list(a = column)))))
  }
  
  df &lt;- data.frame(B = rnorm(10))
  fun1(df, "B")
  fun2(df, "B")
  </code></pre>
  
  <p>I would like to be able to call the function as <code>fun(df, B)</code>, for example. Other options I have considered but have not tried:</p>
  
  <ul>
  <li>Pass <code>column</code> as an integer of the column number. I think this would avoid <code>substitute()</code>. Ideally, the function could accept either.</li>
  <li><code>with(x, get(column))</code>, but, even if it works, I think this would still require <code>substitute</code> </li>
  <li>Make use of <code>formula()</code> and <code>match.call()</code>, neither of which I have much experience with.</li>
  </ul>
  
  <p><em>Subquestion</em>: Is <code>do.call()</code> preferred over <code>eval()</code>?</p>