title: How to get the row sum of a matrix containg characters with corresponding values from another vector
body: <p>Hi I have this column extracted from my data:</p>
  
  <pre><code>x &lt;- data.frame(Category=factor(c("xxyyxyxyx", "xxyyyyxyx", "xxyyxyxyy",
             "yxyyxyxyx", "xxyyxyyyx")))
  &gt; x
    Category
  1 xxyyxyxyx
  2 xxyyyyxyx
  3 xxyyxyxyy
  4 yxyyxyxyx
  5 xxyyxyyyx 
  </code></pre>
  
  <p>I have to calculate the corresponding row sum resulted from each three charaters in each row so I generate this matrix:</p>
  
  <pre><code>xx &lt;- t(apply(x, 1, function(x){strsplit(gsub("([[:alnum:]]{3})", "\\1 ", x), " ")[[1]]}))
  
  &gt; xx
  
           [,1]  [,2]  [,3] 
       [1,] "xxy" "yxy" "xyx"
       [2,] "xxy" "yyy" "xyx"
       [3,] "xxy" "yxy" "xyy"
       [4,] "yxy" "yxy" "xyx"
       [5,] "xxy" "yxy" "yyx"
  </code></pre>
  
  <p>each <code>xx</code> cell corresponds to a value given in this vector</p>
  
  <pre><code>matval=c("xxy"=3, "yxy"=2, "xyx"=7, "xyy"=5, "yyx"=12, "yyy"= 4)
  </code></pre>
  
  <p>I would like based on the matrix <code>xx</code> to add in the matrix <code>x</code> a column containing the sum of each row i.e.,</p>
  
  <pre><code>x
  
     Category    RowSum
   1 xxyyxyxyx     12
   2 xxyyyyxyx     14
   3 xxyyxyxyy     10
   4 yxyyxyxyx     11
   5 xxyyxyyyx     17
  </code></pre>
  
  <p>Many thanks in advance!</p>