Mean of a sliding window in R I have a vector of values that I would like to report the average in windows along a smaller slide.
For example, for a vector of the following values:
4, 5, 7, 3, 9, 8

A window size of 3 and a slide of 2 would do the following:
(4+5+7)/3 = 5.33
(7+3+9)/3 = 6.33
(9+8)/3 = 5.67

And return a vector of these values:
5.33, 6.33, 5.67

Is there a simple function that will do this for me? If it also returned the indices of the window starts that would be an added bonus. In this example that would be 1,3,5
 A: This simple line of code does the thing:
((c(x,0,0) + c(0,x,0) + c(0,0,x))/3)[3:(length(x)-1)]

if x is the vector in question.
A: library(zoo)
x=c(4, 5, 7, 3, 9, 8)
rollmean(x,3)

or
library(TTR)
x=c(4, 5, 7, 3, 9, 8)
SMA(x,3)

A: I can do this easily in Matlab and duck while you downvote me:
%given vector x, windowsize, slide 
idx1 = 1:slide:numel(x);
idx2 = min(numel(x) + 1,idx1 + windowsize);  %sic on +1 here and no -1;
cx = [0;cumsum(x(:))];  %pad out a zero, perform a cumulative sum;
rv = (cx(idx2) - cx(idx1)) / windowsize; %tada! the answer!

as a side effect, idx1 is the index of the element in the sum. I am sure this can be easily translated into R. The idiom first:skip:last in Matlab gives the array first, first+skip, first+2skip, ..., first + n skip, where the last element in the array is no greater than last. 
edit: I had omitted the averaging part (divide by windowsize).
A: shabbychef's answer in R:
slideMean<-function(x,windowsize=3,slide=2){
 idx1<-seq(1,length(x),by=slide);
 idx1+windowsize->idx2;
 idx2[idx2>(length(x)+1)]<-length(x)+1;
 c(0,cumsum(x))->cx;
 return((cx[idx2]-cx[idx1])/windowsize);
}

EDIT: Indices you're looking for are just idx1... this function can be easily modified to return them also, but it is almost equally fast to recreate them with another call to seq(1,length(x),by=slide).
A: Function rollapply in package zoo gets you close:
> require(zoo)
> TS <- zoo(c(4, 5, 7, 3, 9, 8))
> rollapply(TS, width = 3, by = 2, FUN = mean, align = "left")
       1        3 
5.333333 6.333333

It just won't compute the last value for you as it doesn't contain 3 observations. Maybe this will be sufficient for your real problem? Also, note that the returned object has the indices you want as the names of the returned vector.
Your example is making an assumption that there is an unobserved 0 in the last window. It might be more useful or realistic to pad with an NA to represent the missing information and tell mean to handle missing values. In this case we will have (8+9)/2 as our final windowed value.
> TS <- zoo(c(4, 5, 7, 3, 9, 8, NA))
> rollapply(TS, width = 3, by = 2, FUN = mean, na.rm = TRUE, align = "left")
       1        3        5 
5.333333 6.333333 8.500000

A: Rollapply works great with a small dataset. However, if you are working with several million rows (genomics) it is quite slow. 
The following function is super fast.
data <- c(runif(100000, min=0, max=.1),runif(100000, min=.05, max=.1),runif(10000, min=.05, max=1), runif(100000, min=0, max=.2))

slideFunct <- function(data, window, step){
  total <- length(data)
  spots <- seq(from=1, to=(total-window), by=step)
  result <- vector(length = length(spots))
  for(i in 1:length(spots)){
    result[i] <- mean(data[spots[i]:(spots[i]+window)])
  }
  return(result)
}

http://coleoguy.blogspot.com/2014/04/sliding-window-analysis.html
A: This will get you the window means and the index of the first value of the window:
#The data
x <- c(4, 5, 7, 3, 9, 8)

#Set window size and slide
win.size <- 3
slide <- 2

#Set up the table of results
results <- data.frame(index = numeric(), win.mean = numeric())

#i indexes the first value of the window (the sill?)
i <- 1
#j indexes the row of the results to be added next
j <- 1
while(i < length(x)) {
    #This mean preserves the denominator of 3
    win.mean <- sum(x[i:(i+2)], na.rm = TRUE)/win.size
    #Insert the results
    results[j, ] <- c(i, win.mean)
    #Increment the indices for the next pass
    i <- i + slide
    j <- j + 1
    }

Various caveats apply: haven't tested this against anything but your sample data; I believe that appending to data frames like this can get really slow if you have lots of values (because it'll copy the data.frame each time); etc.  But it does produce what you asked for.
A: You are doing a convolution operation. The implementation in R uses FFT internally and you are unlikely to beat it with loops and such things.
> vals=c(4, 5, 7, 3, 9, 8, 0)
> convolve(x=vals, y=c(1, 1, 1)/3, type="filter")
[1] 5.33 5.00 6.33 6.67 5.67

If you want to extract every second result.
> tmp <- convolve(x=vals, y=c(1, 1, 1)/3, type="filter")
> tmp[0:2*2+1]
[1] 5.33 6.33 5.67

