Acredito que o argumento align = "right"
dentro da função zoo:rollmean
seja o que é necessário para resolver esse problema.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(zoo)
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
x <- data.frame(x = c(30,35,10,25,15,20,10))
x %>%
mutate(spread_3 = rollmean(x, k = 3, fill = NA, align = "right"),
spread_5 = rollmean(x, k = 5, fill = NA, align = "right"))
#> x spread_3 spread_5
#> 1 30 NA NA
#> 2 35 NA NA
#> 3 10 25.00000 NA
#> 4 25 23.33333 NA
#> 5 15 16.66667 23
#> 6 20 20.00000 21
#> 7 10 15.00000 16
Created on 2021-02-10 by the reprex package (v1.0.0)