# Moving average (running mean) - how to keep all observations in smoothed time-series?

Let's suppose I have a time-series of 100 daily values and I want to compute a 5-day moving average of this time series.

I would do as follows:

library(igraph)
df=data.frame(x=rnorm(100)) # main time-series
df_mv_avg=data.frame(x=running_mean(df\$x,5)) # time-series smoothed


It is almost obvious that df_mv_avg now contains 4 observations less (96) than df. However, in most examples of smoothed time-series, these time-series have the same length of the original (non-smoothed) time-series (in my case 100 obs).

How can I smooth a time-series (by n values) and at the same time keep the whole length of observations of the original time-series?

Thanks for any help

x <- running_mean(c(rep(x[0],n-1), x, rep(x[length(x)],n-1)), n)