title: Custom Function: Object not found R
body: <p>I have a data.frame called dat.</p>
  
  <pre><code>    colnames(dat)
      [1] "variable"  "weight" 
  </code></pre>
  
  <p>When I run <code>aggregate(weight ~ variable, dat, sum)</code> the function runs without error and returns the values I would expect.</p>
  
  <p>However, when I embed <code>aggregate()</code> within a custom function as follows:</p>
  
  <pre><code>    bins &lt;- function(df, var, wt, n) {
                  tmp &lt;- aggregate(wt ~ var, df, sum)
  
                  ####################
                  other code not shown
                  ####################
  
                  return(tmp)
              }
  </code></pre>
  
  <p>And then run <code>out &lt;- bins(df=dat, var=variable, wt=weight, n=5)</code>, I get the following error message:</p>
  
  <pre><code>    Error in eval(expr, envir, enclos) : object 'weight' not found
  </code></pre>
  
  <p>I tried using <code>with()</code> as well without success.</p>