Sliding window analyses are a common approach when examining very large datasets such as genomic data. A friend is working to find regions of the genome that may show signs of involvement in a complex trait, and part of this analysis required that he calculate the mean value of a measure for each site in the genome. The figure below shows the goal of such a function. Using a window size of 4 and a step size of 2:
There may be a ready made way to do this in R, but I was unable to quickly find it so I just went ahead and wrote one up. First I made some data to work with:
then I wrote a simple function:
and here is the result of running this function and plotting the result with window sizes of 2, 20, 200, and 400.
Hopefully this will be helpful to someone.