title: Is there a nice way to do an operation on all pairings of the columns of two data frames?
body: <p>For example, given the data frames:</p>
  
  <pre><code>&gt; df1
    a b
  1 1 3
  2 2 4
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&gt; df2
     x  y  z
  1 10 12 14
  2 11 13 15
  </code></pre>
  
  <p>and doing an addition operation on each pairing of columns from df1 and df2, I would like to produce:</p>
  
  <pre><code>&gt; df3
    ax bx ay by az bz
  1 11 13 13 15 15 17
  2 13 15 15 17 17 19
  </code></pre>
  
  <p>I wrote the following code which does the job, but I'm wondering if there is a nicer way to do it.</p>
  
  <pre><code>df1 &lt;- data.frame(a=1:2, b=3:4)
  df2 &lt;- data.frame(x=10:11, y=12:13, z=14:15)
  
  byColumnAdditionAllPairs &lt;- function(df1, df2) {
  
      doOp &lt;- function(x, df1, df2, pairs) { 
          i &lt;- pairs[x,1]; # ith column of df1
          j &lt;- pairs[x,2]; # jth column of df2
  
          # add paired columns
          tmp &lt;- df1[i] + df2[j];
  
          # set new column name
          names(tmp)[1] &lt;- paste(names(df1)[i], names(df2)[j], sep="");
  
          # return column
          tmp
      }
  
      # generate column pairings
      pairs &lt;- expand.grid(1:length(df1), 1:length(df2))
  
      # for each column pair, doOp
      data.frame(sapply(1:nrow(pairs), doOp, df1, df2, pairs))
  }
  
  df3 &lt;- byColumnAdditionAllPairs(df1, df2)
  </code></pre>
  
  <p>Thanks,
  Zach</p>