title: R: How to match/join 2 matrices of different dimensions (nrow/ncol)?
body: <p>I want to match/join two matrices, a small one with values should match in a bigger one by rownames/colnames. I only find <a href="https://stackoverflow.com/questions/13651402/r-matching-rows-and-colums-of-matrices-with-different-length">this</a> answer. However, I cannot match the locations as the codeline <code>frn &lt;- as.matrix(bigMatrix[[1]])</code> does not work in my case. The answers of inner, outer ... join <a href="https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right">here</a> did not help, as I want to match/join over a lot of different columns (and not e.g. a CostumerID for X and a CustomerID for y). </p>
  
  <p>As the matrices I use are a 126x104 and a 193x193 matrices. I prepared example data:
  1. the bigger matrix where the smaller one should be included (the letters are in the original data set country names):</p>
  
  <pre><code>a = c("A", "B", "C", "D", "E", "F")
  full_matrix = matrix(nrow = length(a), ncol=length(a))
  dimnames(full_matrix) &lt;- list(levels(as.factor(a)), levels(as.factor(a)))
  
  full_matrix
     A  B  C  D  E  F
  A NA NA NA NA NA NA
  B NA NA NA NA NA NA
  C NA NA NA NA NA NA
  D NA NA NA NA NA NA
  E NA NA NA NA NA NA
  F NA NA NA NA NA NA
  </code></pre>
  
  <p>And the smaller matrix:</p>
  
  <pre><code>matrix = matrix(c(2, 4, 3, 1, 5, 7, 3, 1, 6), nrow=3, ncol=3)
  dimnames(matrix) &lt;- list(c("B","C","E"), c("A","B","F"))
  
  matrix
    A B F
  B 2 1 3
  C 4 5 1
  E 3 7 6
  </code></pre>
  
  <p>The result should look so:</p>
  
  <pre><code>   A  B  C  D  E  F
  A NA NA NA NA NA NA
  B  2  1 NA NA NA  3
  C  4  5 NA NA NA  1
  D NA NA NA NA NA NA
  E  3  7 NA NA NA  6
  F NA NA NA NA NA NA
  </code></pre>