How can I plot the mean of sliding groups of data in R?

2.6k views Asked by At

I have a frequency distribution I am trying to plot in R. The dataset is quite noisy however and results in an untidy plot.

I would like to take the mean of 10 unit sliding windows and plot that data instead. Hopefully this will clean up the plot, by reducing the number of data points?

I have tried scatter.smooth() and loess(), but these don't do what I want. I could code this myself, but wondered if anyone was aware of a function that might save the time?

1

There are 1 answers

0
gawbul On BEST ANSWER

I found this whilst continuing my search on Google - This appears to solve my problem :D

https://stats.stackexchange.com/questions/3051/mean-of-a-sliding-window-in-r

Edit:

I'm using the following

library(zoo)
quartz()
plot(rollmean(freq_dist, 10))

Works like a dream :-D

I did an install.packages("zoo", dependencies=TRUE) in order to get the zoo library into R!