title: R-Using a list of Indices to calculate the mean of a group of values in several columns of a data frame
body: <p>i have a list of indices that i want to use to calculate a mean of a fix number of rows starting from my index. Ideally, i would find a way to do this for several columns at the same time.</p>
  
  <p>My list of indices looks something like this:</p>
  
  <pre><code>a = seq(1,100,10)
  </code></pre>
  
  <p>My data frame is similar to:</p>
  
  <pre><code>df&lt;-data.frame(A= 0:10, b=10:20, c = 20:30, d=40:50, e = seq(0,100,10))
      A  b  c  d   e
  1   0 10 20 40   0
  2   1 11 21 41  10
  3   2 12 22 42  20
  4   3 13 23 43  30
  5   4 14 24 44  40
  6   5 15 25 45  50  etc...
  </code></pre>
  
  <p>What i would like would be a way, to get means out of this data frame, for all columns in a manner like:</p>
  
  <pre><code>colnames = c("A", "b", "c")
  
      means(df[(a-4):(a), colnames])
  </code></pre>
  
  <p>Now obviously that doesn't work, but i hope it illustrates my idea. I want a mean of the last 5 entrys counting backwards from <code>a</code>.</p>
  
  <p>Now i have tried with the <code>foreach</code> package (probably wrongly so) and if i use</p>
  
  <pre><code>library(foreach)
  test &lt;- foreach(i = a) %do% mean(df[(i-4):(i),"A"])
  </code></pre>
  
  <p>i get a working result, which is a list with the means i want, but only for column "A".
  If i instead use</p>
  
  <pre><code>test &lt;- foreach(i = a, j = colnames) %do% mean(df[(i-4):(i),j])
  </code></pre>
  
  <p>i somehow only get one value per column.</p>
  
  <p>I have looked into solutions with <code>sapply and tapply</code> but i am either too stupid to understand how to use those iteratively for parts of a column or they are just not made for that.</p>
  
  <p>Lastly people regularly come up with <code>data.table</code>, but i quite honestly have no idea how the syntax works in this case. I know that</p>
  
  <pre><code>DT &lt;- as.data.table(df)
  DT[,mean(A[(a-4):(a)])]
  </code></pre>
  
  <p>returns</p>
  
  <pre><code>numerical expression has 10 elements: only the first used
  </code></pre>
  
  <p>which was the problem in the first place with <code>mean</code>. If i try to use my colnames parameter, it gets even worse</p>
  
  <pre><code>DT[,mean(colnames[(a-4):(a)]), with = FALSE]
  argument is not numeric or logical: returning NA
  </code></pre>
  
  <p>I am sorry if I am posting an obvious question, but hours of search here and elsewhere on the internet and in R reference documents and the documentation have left me with the feeling that this is not that easy.</p>
  
  <p>Any help would be greatly appreciated!!!</p>
  
  <p>P.S.: Should it help in any way, <code>a</code> is derived from evaluating other parts of the dataframe like <code>a = which(thetruth &amp; nothingbut)</code> and could if necessary also be used as logical. I just think i have more overview with this construction.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I wanted a subset of data in both column as in several rows defined by a list.
  The solution</p>
  
  <pre><code>do.call(rbind, lapply(a, function(x) colMeans(DT[(x-4):x,colNames], na.rm=T)))
  </code></pre>
  
  <p>works as well as</p>
  
  <pre><code>rbindlist(lapply(a, function(x) DT[(x-4):x, lapply(.SD, mean), .SDcols = colnames]))
  </code></pre>
  
  <p>If there is a date column with class <code>POSIXct</code> the function <code>colMeans</code> cannot process it, which means you either convert your date into numeric, or use the <code>data.table</code> solution</p>