title: How to loop through a matrix rows from i:n and columns
body: <p>I am still a beginner in r. I am trying to loop through a matrix and calculate sums looping from every single row(1:5) until the threshold (e.g. 14) within this row is reached. Then go through all columns (2:6). What I want is, that my matrix(b) is filled with the numbers(91:95), where the rows have reached the threshold.  I've tried this. Has anyone an idea, what I'm doing wrong. </p>
  
  <pre><code>a &lt;- matrix(c(91:95,1:25), ncol=6)    
  b &lt;- matrix(NA,ncol=6,nrow=5)
  
  
  for(i in 1:nrow(a)) {
      for(j in 2:ncol(a)) {
          for(n in 1:5) {
              if(sum(a[seq(i:n),j])&gt;14) {
                  b[i,j] &lt;- a[n,1]
                  break
              }
          }
      }
  }
  </code></pre>
  
  <p>This is my result. </p>
  
  <pre><code>      [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]   NA   95   93   92   91   91
  [2,]   NA   NA   94   91   91   91
  [3,]   NA   NA   91   91   91   91
  [4,]   NA   NA   91   91   91   91
  [5,]   NA   91   91   91   91   91
  </code></pre>
  
  <p>My desired result however should look like this. Results should be on the starting point(i) and show the row (n) where the threshold(14) has been exceed. </p>
  
  <pre><code>      [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]   NA   95   93   92   91   91
  [2,]   NA   NA   93   93   91   91
  [3,]   NA   NA   94   94   91   91
  [4,]   NA   NA   95   95   91   91
  [5,]   NA   NA   NA   95   91   91
  </code></pre>
  
  <p>Can anyone help me with this. </p>
  
  <p>Thanks in advance. Also is easier ideas than nested loops are appreciated.  </p>