title: r how to subtract elements of different length matrices
body: <p>I've got two dataframes that have same number of rows (22 rows) and different number of columns.</p>
  
  <p>sim_10(22 rows, 15 columns): </p>
  
  <pre><code>2    0.577967    0.023869   0.021571    0.481754    0.61584     0    0   0  0  0     0.024057    0.014209   1    0.085784
  
  
  8   0.0775       0.274113   2.7e-05     0.01215     0.009345    0    0  0   0   0   0.004092     0.00784    0     0
  </code></pre>
  
  <p>And how can I do it in easy way..
       ...</p>
  
  <p>nm_10(22 rows, 8 columns)</p>
  
  <pre><code>11  0.926554    0.256966    0.859375    0   0.191011    0   0           0
  2   0.858757    0.256966    0.21875     0   0.662921    0   0.845506    0.090909
   ..
  </code></pre>
  
  <p>the first column of two dataframes are same just in different order(names of cases). I need to find the matching row names in nm_10 and sm_10 and subtract every element of sm_10 in that row to the every element in the nm_10. example:</p>
  
  <p>for '2' sm_nm_10:</p>
  
  <pre><code>  2 (0.577967-0.858757=-0.28079)    (0.577967-0.256966=)    (0.577967-0.21875)  ...(0.577967-0.090909=..)
  
      (0.023869-0.858757=)    (0.023869-0.256966=)    (0.023869-0.21875)  ...(0.023869-0.090909=..)
  
   ....
  
  (0.085784-0.858757=)    (0.085784-0.256966=)    (0.085784-0.21875)  ...(0.085784-0.090909=..)
  </code></pre>
  
  <p>and that for all data. 
  Check every row's first column, find matching row and do operation. 
  Is there any simple way to do it? I looked into sweep, apply but couldn't figure out how to use them. I keep getting errors referring to length etc. I decided to keep it simple and here is what I have :</p>
  
  <pre><code>s = numeric()
  
  for (i in 1:nrow(sm_10))
  {
  for (jj in 1:nrow(nm_10))
  { 
   for (j in 2:ncol(nm_10))
   {
    for (ii in 2:ncol(sm_10))
   {
   sm_10[i,]%in% nm_10[jj,]
  
  s &lt;- sm_10[,ii]-nm_10[,j]
    }}}}
  </code></pre>
  
  <p>What is wrong here? Could anyone explain and suggest better?</p>
  
  <p>UPDATE:</p>
  
  <p>The end result I need is all rows 22 with the elements subtractions. that is 22 rows with (14*7 ) columns..</p>