title: R: summing over an interval of rows
body: <p>I'm a beginner R user and I need to write a function that sums the rows of a data frame over a fixed interval (every 4 rows).
  I've tried the following code</p>
  
  <pre><code>camp&lt;-function(X){
    i&lt;-1
    n&lt;-nrow(X)
    xc&lt;-matrix(nrow=36,ncol=m)
    for (i in 1:n){
      xc&lt;-apply(X[i:(i+4),],2,sum)
      rownames(xc[i])&lt;-rownames(X[i])
      i&lt;-i+5
    }
    return(xc)
  }
  </code></pre>
  
  <p>the result is "Error in X[i:(i + 4), ] : index out of range".
  How can I solve? Any suggestion?</p>
  
  <p>Thanks.</p>