title: Calculate row-wise matrix cumsum from vector
body: <p>Given a vector I would like to create a square matrix where elements of vector are on diagonal and there is row-wise cumsum of elements.</p>
  
  <p>Example vector:</p>
  
  <pre><code>vec &lt;- c(1, 2, 3, 4)
  </code></pre>
  
  <p>Required output:</p>
  
  <pre><code>     [,1] [,2] [,3] [,4]
  [1,]    1    3    6   10
  [2,]    0    2    5    9
  [3,]    0    0    3    7
  [4,]    0    0    0    4
  </code></pre>
  
  <p>Now, I am using double for loop function:</p>
  
  <pre><code>diagSum &lt;- function(vec) {
    mat &lt;- diag(vec)
    for (i in seq(nrow(mat))) {
      for (j in seq(i, ncol(mat))) {
        if (j &gt; i) {
          mat[i, j] &lt;- mat[i, j - 1] + mat[j, j]      
        }
      }
    }
    mat
  }
  </code></pre>
  
  <p>What would be R-way (avoiding for loops) of doing this?</p>