title: Trying to create a matrix with transition rules in R
body: <p>I am new at R and I am trying to create a matrix like the following:
  <a href="https://i.stack.imgur.com/k0X4O.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>The logic is that for every row (x), if value corresponds to the 1st column (y=0), then one point is subtracted. Else, for every step that value makes to the right for the rest columns (y>=1), two points are added with max value=8. 
  What I have tried is the following but it does not work properly:</p>
  
  <pre><code>m=9
  n=5
  test=matrix(0,m,n)
  rownames(test) &lt;- c("0","1","2","3","4","5","6","7","8")
  colnames(test) &lt;- c("O","1","2","3","&gt;=4")
  test
  
  for (i in 1:dim(test)[1]) {
    for (j in 1:dim(test)[2]) {
      if (j&lt;=1) {
        test[i,j] = i-2
      }
      else
      {
        test[i,j] = i+2
      }
    }
  }
  test[test &gt; 8] &lt;- 8
  test[test &lt; 0] &lt;- 0
  print (test)
  
  </code></pre>
  
  <p>Any advice or help would be much appreciated. </p>