Here is one way.  I took your data and applied the 'approx' function to get
evenly spaced values and then 'filter' to create a moving average of a
window of size 5.


set.seed(1)
x = c(0,1, 3,3.4, 5, 10, 11.23)
y = x**2 + rnorm(length(x))
rbind(x, y)

# create 'even' spacing using the 'approx' function
steps <- seq(min(x), max(x), length = 100)
newData <- approx(x, y, xout = steps)

# use filter for running average

avg <- filter(newData$y, rep(1/5, 5))

#plot the data
plot(x, y, type = 'o')

lines(newData$x, avg, col = 'red')



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Thu, Mar 27, 2014 at 12:35 PM, Luca Cerone <luca.cer...@gmail.com> wrote:

> Dear all,
> I have a vector x and a vector y = f(x).
> e.g.
>
> x = c(0,1, 3,3.4, 5, 10, 11.23)
> y = x**2 + rnorm(length(x))
>
> I would like to apply a moving average to y, knowing that the x values
> are not uniformly spaced. How can I do this in R?
> What alternatives I have to filter out the noise from Y??
>
> Thanks in advance for the help!
>
>
>
>
> Luca Cerone
>
> Tel: +34 692 06 71 28
> Skype: luca.cerone
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to