title: R - Multiply a Dataframe by Another dataframe
body: <p>I have 2 data frames df1 and df2. df1 and df2 have the same size (rows and columns) and same factors. Say:</p>
  
  <pre><code>df1 &lt;- data.frame(a=c('alpha','beta','gamma'), b=c(1,2,3), c=c('x','y','z'), d=c(4,5,6))
  
        a b c d
  1 alpha 1 x 4
  2  beta 2 y 5
  3 gamma 3 z 6
  </code></pre>
  
  <p>and </p>
  
  <pre><code>df2 &lt;- data.frame(a=c('alpha','beta','gamma'), b=c(7,8,9), c=c('x','y','z'), d=c(10,11,12))
  
        a b c  d
  1 alpha 7 x 10
  2  beta 8 y 11
  3 gamma 9 z 12
  </code></pre>
  
  <p>I would like to multiply these 2 dataframes and get a result like tyhis:</p>
  
  <pre><code>      a b  c d
  1 alpha 7  x 40
  2  beta 16 y 55
  3 gamma 27 z 72
  </code></pre>
  
  <p>I have done some search and attempted the following code:</p>
  
  <pre><code>M &lt;- merge(df1,df2,by=c('a','c'))
  S &lt;- M[,grepl("*\\.x$",names(M))] * M[,grepl("*\\.y$",names(M))]
  cbind(M[,c('a','c'),drop=FALSE],S)
  </code></pre>
  
  <p>this code works fine and gives the following:</p>
  
  <pre><code>      a c b.x d.x
  1 alpha x   7  40
  2  beta y  16  55
  3 gamma z  27  72
  </code></pre>
  
  <p><strong>Question:</strong> Is there a better way to achieve this multiplication ? Keep in mind that my dataframes have same number of rows, columns and factor names. My real life dataframes are much larger, both rows and columns.</p>