-2

I have got a data.frame where one column represents dates in years and the other column observations of e.g. sea level in mm.

I need to calculate the 10-year smoothed mean.

Here some fake data:

x = rnorm(1:100) #annual sea leavel rise
date = seq(1801,1900) #years from 1801 to 1900

df = data.frame(date,x) #create data.frame

Is there any R function that could help? Is the smoothed mean the same as the moving average?

Thanks for any help and/or suggestion

aaaaa
  • 149
  • 2
  • 18
  • 44

1 Answers1

1

The moving average is just the simplest case of the smoothed mean, which is widely used in the climate science. The R filter function which may be quite a convenient way to resolve your issue

# sample data
x <- rnorm(1:100)
date <- seq(1801,1900)
df <- data.frame(date,x)
# coefficients for moving average are the simplest ones
f10 <- rep(1/10,10)
df[,"x_10ma"] <- filter(df$x, f10, sides = 1)
# fast check
plot(x = df$date, y = df$x, col="red")
points(x = df$date, y = df$x_10ma,col="blue")

More advanced smoothing options are provided, e.g. by the 'TTR' or 'smooth' packages.

Ekatef
  • 1,061
  • 1
  • 9
  • 12