title: Averaging rows with changing number of columns
body: <p>My data set has 60 columns and several hundred observations. Each observation has a certain length (depending on the length of the video that has been analyzed) and the last few columns might be just NA.
  I want to be able to average a portion of values for each row. For example, if the video length is 15 seconds, I need to average the first 3  seconds
  ( a fifth of the row) and if it is 60 seconds, I need the first 12 seconds average.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">obs</th>
  <th style="text-align: center;">veideolength</th>
  <th style="text-align: center;">sec1</th>
  <th style="text-align: center;">sec2</th>
  <th style="text-align: center;">sec3</th>
  <th style="text-align: center;">sec4</th>
  <th style="text-align: center;">sec5</th>
  <th style="text-align: center;">sec6</th>
  <th style="text-align: center;">sec7</th>
  <th style="text-align: center;">...</th>
  <th style="text-align: center;">sec60</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">obs1</td>
  <td style="text-align: center;">10</td>
  <td style="text-align: center;">15</td>
  <td style="text-align: center;">251</td>
  <td style="text-align: center;">281</td>
  <td style="text-align: center;">249</td>
  <td style="text-align: center;">294</td>
  <td style="text-align: center;">278</td>
  <td style="text-align: center;">249</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">na</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs2</td>
  <td style="text-align: center;">5</td>
  <td style="text-align: center;">205</td>
  <td style="text-align: center;">182</td>
  <td style="text-align: center;">164</td>
  <td style="text-align: center;">178</td>
  <td style="text-align: center;">252</td>
  <td style="text-align: center;">na</td>
  <td style="text-align: center;">na</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">na</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs3</td>
  <td style="text-align: center;">55</td>
  <td style="text-align: center;">157</td>
  <td style="text-align: center;">270</td>
  <td style="text-align: center;">277</td>
  <td style="text-align: center;">258</td>
  <td style="text-align: center;">233</td>
  <td style="text-align: center;">242</td>
  <td style="text-align: center;">181</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">na</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs4</td>
  <td style="text-align: center;">60</td>
  <td style="text-align: center;">169</td>
  <td style="text-align: center;">194</td>
  <td style="text-align: center;">154</td>
  <td style="text-align: center;">173</td>
  <td style="text-align: center;">237</td>
  <td style="text-align: center;">214</td>
  <td style="text-align: center;">257</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">187</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs5</td>
  <td style="text-align: center;">30</td>
  <td style="text-align: center;">187</td>
  <td style="text-align: center;">159</td>
  <td style="text-align: center;">222</td>
  <td style="text-align: center;">235</td>
  <td style="text-align: center;">275</td>
  <td style="text-align: center;">196</td>
  <td style="text-align: center;">169</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">na</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs6</td>
  <td style="text-align: center;">20</td>
  <td style="text-align: center;">198</td>
  <td style="text-align: center;">254</td>
  <td style="text-align: center;">227</td>
  <td style="text-align: center;">247</td>
  <td style="text-align: center;">210</td>
  <td style="text-align: center;">193</td>
  <td style="text-align: center;">289</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">na</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs7</td>
  <td style="text-align: center;">60</td>
  <td style="text-align: center;">198</td>
  <td style="text-align: center;">271</td>
  <td style="text-align: center;">225</td>
  <td style="text-align: center;">157</td>
  <td style="text-align: center;">205</td>
  <td style="text-align: center;">192</td>
  <td style="text-align: center;">170</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">223</td>
  </tr>
  <tr>
  <td style="text-align: left;">obs8</td>
  <td style="text-align: center;">25</td>
  <td style="text-align: center;">261</td>
  <td style="text-align: center;">240</td>
  <td style="text-align: center;">263</td>
  <td style="text-align: center;">230</td>
  <td style="text-align: center;">153</td>
  <td style="text-align: center;">267</td>
  <td style="text-align: center;">249</td>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">na</td>
  </tr>
  <tr>
  <td style="text-align: left;">…</td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  <td style="text-align: center;"></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have tried <code>rowMeans</code> but the problem is it does not accept a variable inside its arguments.</p>
  <pre><code>df$average1 &lt;-rowMeans(df[,3:(3+floor(df$videolength/5))])
  </code></pre>
  <p>I also have tried for loop, but the variable <code>j</code> does not update and remains the first variable that it has been assigned.</p>
  <pre><code>for(i in 1:nrow(df)){
    j = (floor(df$VideoLength[i]/5)-1)
    frames1$average1 &lt;-rowMeans(df[,3:(3+j)])
  }
  </code></pre>