title: How to replace elements in char matrix with values from named vector in R
body: <p>I have a char matrix and a named vector of doubles where the vector names correspond to the elements in the char matrix. How can I replace the matrix elements by their corresponding doubles?</p>
  <pre><code>named_vec &lt;- setNames(c(1,2,3), c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;))
  a b c 
  1 2 3 
  
  char_mat &lt;- matrix(c(&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;c&quot;), nrow = 2)
       [,1] [,2]
  [1,] &quot;a&quot;  &quot;b&quot; 
  [2,] &quot;b&quot;  &quot;c&quot; 
  </code></pre>
  <p>The resulting matrix should look like this</p>
  <pre><code>result_mat &lt;- matrix(c(&quot;1&quot;,&quot;2&quot;,&quot;2&quot;,&quot;3&quot;), nrow = 2)
       [,1] [,2]
  [1,] &quot;1&quot;  &quot;2&quot; 
  [2,] &quot;2&quot;  &quot;3&quot; 
  </code></pre>