title: How do I apply a multi-parameter function in R?
body: <p>I have the following data frame and vector.</p>
  
  <pre><code>&gt; y
    v1 v2 v3
  1  1  6 43
  2  4  7  5
  3  0  2 32
  
  &gt; v
   [1] 1 2 3
  </code></pre>
  
  <p>I want to apply the following function to every <b>ROW</b> in that data frame such that v is added to every <b>ROW</b> of y:</p>
  
  <pre><code>x &lt;- function(vector1,vector2) {
      x &lt;- vector1 + vector2
  }
  </code></pre>
  
  <p>... in order to get THESE results: </p>
  
  <pre><code>  v1 v2 v3
  1  2  8 46
  2  5  9  8
  3  1  4 35
  </code></pre>
  
  <p>mapply applies the function to <b>COLUMNS</b>:</p>
  
  <pre><code>&gt; z &lt;- mapply(x, y, MoreArgs=list(vector2=v))
  &gt; z
       v1 v2 v3
  [1,]  2  7 44
  [2,]  6  9  7
  [3,]  3  5 35
  </code></pre>
  
  <p>I've tried transposing the data frame so that the function will be applied to rows and not columns, but mapply gives me weird results after transposing:</p>
  
  <pre><code>&gt; transposed &lt;- t(y)
  &gt; transposed
     [,1] [,2] [,3]
  v1    1    4    0
  v2    6    7    2
  v3   43    5   32
  
  &gt; z &lt;- mapply(x, transposed, MoreArgs=list(vector2=v))
  &gt; z
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
  [1,]    2    7   44    5    8    6    1    3   33
  [2,]    3    8   45    6    9    7    2    4   34
  [3,]    4    9   46    7   10    8    3    5   35
  </code></pre>
  
  <p>...Help?</p>
  
  <p>############################ EDIT #########################</p>
  
  <p>Thanks for all the answers!  I'm learning tons of new R functions that I've never seen before, which is fantastic. </p>
  
  <p>I want to clarify my earlier question a bit.  What I'm really asking is a much more general question - how to apply a multi-parameter function to each row in R (at the moment, I'm tempted to conclude that I should just use a loop, but I would like to figure out if it IS possible, just for future reference...)  (I also purposefully refrained from showing the code I'm working with since it's kind of messy).</p>
  
  <p>I tried using the sweep function as was suggested, but I get the following error: </p>
  
  <pre><code>testsweep &lt;- function(vector, z, n) {
     testsweep &lt;- z
  }
  &gt; n &lt;- names(Na_exp)
  &gt; n
  [1] "NaCl.10000.2hr.AVG_Signal" "NaCl.10000.4hr.AVG_Signal"
  
  
  &gt; t &lt;- head(Li_fcs,n=1)
  &gt; t
    LiCl.1000.1hr.FoldChange LiCl.2000.1hr.FoldChange LiCl.5000.1hr.FoldChange
  [1,]              -0.05371838               -0.1010928              -0.01939986
       LiCl.10000.1hr.FoldChange LiCl.1000.2hr.FoldChange
  [1,]                 0.1275617                -0.107154
      LiCl.2000.2hr.FoldChange LiCl.5000.2hr.FoldChange
  [1,]              -0.06760782              -0.09770226
      LiCl.10000.2hr.FoldChange LiCl.1000.4hr.FoldChange
  [1,]                -0.1124188              -0.06140386
      LiCl.2000.4hr.FoldChange LiCl.5000.4hr.FoldChange
  [1,]              -0.04323497              -0.04275953
      LiCl.10000.4hr.FoldChange LiCl.1000.8hr.FoldChange
  [1,]                0.03633496               0.01879461
      LiCl.2000.8hr.FoldChange LiCl.5000.8hr.FoldChange
  [1,]                 0.257977              -0.06357423
      LiCl.10000.8hr.FoldChange
  [1,]                0.07214176
  
  
  &gt; z &lt;- colnames(Li_fcs)
  &gt; z
   [1] "LiCl.1000.1hr.FoldChange"  "LiCl.2000.1hr.FoldChange" 
   [3] "LiCl.5000.1hr.FoldChange"  "LiCl.10000.1hr.FoldChange"
   [5] "LiCl.1000.2hr.FoldChange"  "LiCl.2000.2hr.FoldChange" 
   [7] "LiCl.5000.2hr.FoldChange"  "LiCl.10000.2hr.FoldChange"
   [9] "LiCl.1000.4hr.FoldChange"  "LiCl.2000.4hr.FoldChange" 
  [11] "LiCl.5000.4hr.FoldChange"  "LiCl.10000.4hr.FoldChange"
  [13] "LiCl.1000.8hr.FoldChange"  "LiCl.2000.8hr.FoldChange" 
  [15] "LiCl.5000.8hr.FoldChange"  "LiCl.10000.8hr.FoldChange"
  </code></pre>
  
  <p>But when I try to apply sweep...</p>
  
  <pre><code>&gt; test &lt;- sweep(t, 2, z, n, FUN="testsweep")
  Error in if (check.margin) { : argument is not interpretable as logical
  In addition: Warning message:
  In if (check.margin) { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>When I remove the n parameter from this test example, sweep works fine.  This suggests to me that sweep cannot be used unless the all parameters provided to sweep are either the same number of columns as the t vector, or of length 1.   Please correct me if I am mistaken...</p>