title: Creating the mean average of every nth object in a specific column of a dataframe
body: <p>I am trying to average every n-th object of a specific column in a dataframe using the following code. I understand that using the for-loop is computationally inefficient. This is why I would like to ask whether there is a more efficient way to create the average of every n-th row? My data looks a little bit like this.</p>
  
  <pre><code>set.seed(6218)
  n &lt;- 8760
  s1 &lt;- sample(30000:70000, n)
  s2 &lt;- sample(0:10000, n)
  inDf &lt;- cbind(s1, s2)
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>I call h_average like this:
  h_average(inDf, 24, 1, 1)
  This would mean that I average every first point of "every" 24 point subset. So the points 1, 25, 49, 73,... Also I only do this for the first column.</p>
  
  <p>Thanks in advance,
  BenR  </p>
  
  <pre><code>#' h_average
  #' 
  #' Computing the average of every first, second, third, ... hour of the day/week
  #' 
  #' @param data merged data
  #' @param tstep hour-step representing the number of hours for a day/week
  #' @param h hour, which should be averaged. Should be between 1 - 24/1 - 168.
  #' @param x column number
  #' @return mean average of the specific hour
  h_average &lt;- function(data, tstep, h, x) {
    sum_1 &lt;- 0
    sum_2 &lt;- 0
    mean  &lt;- 0
  
    for (i in seq(h, nrow(data), tstep)){
      if(data[i,x]){
        sum_1 &lt;- sum_1 + 1
        sum_2 &lt;- sum_2 + data[i,x]
      }
    }
    mean &lt;- sum_2/sum_1
    return(mean)
  }
  </code></pre>