title: How to replace values with their mean?
body: <p>I have two matrices</p>
  
  <pre><code> A = matrix(c(2, 2, 2, 3, 3, 3),nrow=3,ncol=2) 
  &gt; A
        [,1] [,2]
  [1,]    2    3
  [2,]    2    3
  [3,]    2    3
  B = matrix(c(2, 4, 3, 1, 5, 7),nrow=3, ncol=2) 
  &gt; B 
        [,1] [,2]
  [1,]    2    1
  [2,]    4    5
  [3,]    3    7
  </code></pre>
  
  <p>take the mean of all values in B that correspond to 3 in B:
  Create a matrix with only the means:
  Wanted matrix:</p>
  
  <pre><code>    C
  
          [,1] [,2]
   [1,]    3    4.3
   [2,]    3    4.3
   [3,]    3    4.3
  </code></pre>