title: Add column to data frame using transform and calling a function -- an annoying weird behavior
body: <p><em><strong>Reproducible and simplified example to explain my core question</em></strong>:</p>
  
  <p>I start with a function having a vector as argument, say: <code>fVec &lt;- function(v) v[1]*v[2]</code>.</p>
  
  <p>For comparison purposes, a direct way with explicit parameters to express the same function is: <code>fDirect &lt;- function(a, b) a*b</code>.</p>
  
  <p>In my use case, I want to build a function with explicit parameters like <code>fDirect</code> above but implement it by calling the initial function <code>fVec</code>.  So for this purpose, I defined: <code>fIndirect &lt;- function(a, b) fVec(c(a, b))</code>.  (I explain below why I want to do something like that apparently not making any sense!)</p>
  
  <p>As expected, <code>fVec(c(2, 3))</code>, <code>fDirect(2, 3)</code>, and <code>fIndirect(2, 3)</code> return 6, so far so good.</p>
  
  <p>Now for plotting purposes, I build a data frame with the data I want to plot as follows:</p>
  
  <ol>
  <li>Create the function parameter values: <code>mydf &lt;- data.frame(a=1:3, b=2:4)</code>.</li>
  <li>Add the function value in a new column using <code>transform</code>.</li>
  </ol>
  
  <p>Using <code>fDirect</code>, <code>transform(mydf, v=fDirect(a, b))</code> does work as expected, it returns:</p>
  
  <pre><code>&gt; transform(mydf, v=fDirect(a, b))
    a b  v
  1 1 2  2
  2 2 3  6
  3 3 4 12
  </code></pre>
  
  <p>However, using <code>fIndirect</code>, it does not return the desired function values:</p>
  
  <pre><code>&gt; transform(mydf, v=fIndirect(a, b))
    a b v
  1 1 2 2
  2 2 3 2
  3 3 4 2
  </code></pre>
  
  <p>In debugging, I realized using <code>fIndirect</code>, <code>transform</code> passed to <code>fVec</code> a vector argument being the union of columns <code>a</code> and <code>b</code> of the data frame, that is: <code>c(mydf[["a"]], mydf[["b"]])</code>.  As a result, <code>fVec</code> did what it was programmed to do, that is evaluate the product of the first two elements, hence returning <code>1*2=2</code> for all rows.</p>
  
  <p>So far, the best <strong>solution</strong> I could come up with to work around this <code>transform</code> challenge was using <code>apply</code> as follows:</p>
  
  <pre><code>cbind(mydf, v=apply(mydf, 1, function(row) fIndirect(row["a"], row["b"])))
  </code></pre>
  
  <p><em><strong>Question</em></strong>:
  Why does <code>transform</code> passes both data frame columns to <code>fVec</code> through <code>fIndirect</code> instead of behaving the same way as when calling it with <code>fDirect</code> where there it evaluates the function one row at a time?  Is it an R bug or do I misunderstand something fundamental in the way R works like perhaps something about scoping and/or argument casting?</p>
  
  <p><em><strong>Context</em></strong>:</p>
  
  <p>This section explains why I follow such a process, perhaps someone can point out a better way to architect it.</p>
  
  <p>I have a fairly complex objective function I try to optimize (i.e., <code>fVec</code> role).  This function has a variable number of parameters passed in as a named vector argument for convenience in the various ways I use this function, in particular the use of BBoptim optimizer that expects a vector as argument of the objective function.</p>
  
  <p>In some cases where the number of variable parameters is 1 or 2, I want to plot my objective function (I use <code>plot</code> in 1-dim case; I use <code>levelplot</code> and <code>wireframe</code> from the lattice package in the 2-dim case).</p>
  
  <p>So then, I build a temporary function with explicit parameters (i.e., <code>fIndirect</code> role) for convenience of building the data I want to plot into a data frame (i.e., <code>mydf</code> role).  Since my objective function is relatively complex and I need a version with a vector argument, I would like my temporary function <code>fIndirect</code> to be implemented by calling my original objective function <code>fVec</code>.</p>
  
  <p>Can anyone propose a better way to accomplish the same goal than the process I showed in my simplified example above?</p>