title: How to look up the value of a row/column combination in a matrix (R)?
body: <p>I have the following look up problem in R (but I am not sure whether I use this term 100% correctly). Given is a <code>matrix</code> with data points, where row and column names are identical and in the same order (as in e.g., a covariance matrix). Also given is a <code>data.frame</code> of row and column name pairs for which the corresponding value should be looked up in the matrix.</p>
  
  <p>To illustrate (and using a non-symmetric matrix for generality):</p>
  
  <pre><code>set.seed(1)
  m = matrix(1:25,5,5)
  colnames(m) &lt;- c("A","B","C","D","E")
  rownames(m) &lt;- c("A","B","C","D","E")
  
  l &lt;- matrix(ncol=2,nrow=5)
  for(i in 1:5){
    l[i,] &lt;- sample(c("A","B","C","D","E"),2,replace = FALSE) #choose TRUE if diagonal elements should be included in the list
  }
  l &lt;- as.data.frame(l)
  colnames(l) &lt;- c("row","column")
  </code></pre>
  
  <p>So we have matrix ´m´ and data.frame <code>l</code> (the equal number of rows of <code>m</code> and <code>l</code> are coincidental and <code>nrow(l)</code> could be much higher, though redundant pairs surely occur for <code>&gt;25</code>):</p>
  
  <pre><code>  A  B  C  D  E
  A 1  6 11 16 21
  B 2  7 12 17 22
  C 3  8 13 18 23
  D 4  9 14 19 24
  E 5 10 15 20 25
  
    row column
  1   B      E
  2   C      D
  3   B      D
  4   E      C
  5   D      A
  </code></pre>
  
  <p>And we seek an algorithm that finds:</p>
  
  <pre><code>&gt; c(22,18,17,15,4)
  </code></pre>
  
  <p>I'd be happy to pointers how this problem is referred correctly to and practical solutions.</p>