title: R - Execute Zoo rollapply/rollmean on multiple columns
body: <p>I want to calculate a rolling mean (backward and forward) over 15 days each. Here is a testframe:</p>
  
  <pre><code>date_list = seq(ymd('2000-01-15'),ymd('2010-09-18'),by='day')
  testframe = data.frame(Date = date_list)
  testframe$Day = substr(testframe$Date, start = 6, stop = 10)
  testframe$V1 = runif(3900, 2.0, 35.0)
  testframe$V2 = runif(3900, 5.0, 40.0)
  testframe$V3 = runif(3900, -10.0, 10.0)
  testframe$V4 = seq(from = 5, to = 45, length.out = 3900)
  </code></pre>
  
  <p>I know how to calculate it for each individual column:</p>
  
  <pre><code>library(zoo)
  rollmean(testframe$V4, 31)
  rollapply(testframe$V4, 31, mean)
  </code></pre>
  
  <p>But how can I do this for each column at once? I think I have to exclude the Day and Date column for that, but how can I do that within the command? And how can I get the results in my old testframe with NAs for the first and last 15 days?</p>
  
  <p>I tried this: </p>
  
  <pre><code>testframe[paste0("new_col",1:4)] &lt;- lapply(testframe[,3:6], rollapply, FUN = mean, width = 31)
  </code></pre>
  
  <p>But it doesnt work!</p>