title: Writing Functions With a "data" Argument
body: <p>I want to write a function that can take columns within a data frame or column names and the data frame they come from as arguments.</p>
  
  <pre><code>df &lt;- data.frame(x = c(1:5), y = c(6:10), z = LETTERS[1:5])
  
  my_fxn &lt;- function (aaa, bbb, ccc, data) {
    if (!missing(data)) {
      aaa = as.numeric(data$aaa)
    }
    print(aaa[1])
  }
  </code></pre>
  
  <p>This function works fine when I provide columns from the data frame as the arguments:</p>
  
  <pre><code>&gt; my_fxn(df$x, df$y, df$z, df)
  [1] 1
  </code></pre>
  
  <p>It does not work if the column names and the data frame name are different from the names of the function's arguments:</p>
  
  <pre><code>&gt; my_fxn(x, y, z, df)
  [1] NA
  </code></pre>
  
  <p>It does, however, work when the column names and the data frame name are the same as the names of the function's arguments:</p>
  
  <pre><code>data &lt;- df
  names(data) &lt;- c("aaa", "bbb", "ccc")
  &gt; my_fxn(aaa, bbb, ccc, data)
  [1] 1
  </code></pre>
  
  <p>What is going on?</p>
  
  <p>Thanks!</p>