I believe the argument align = "right"
within the function zoo:rollmean
whatever is needed to solve this problem.
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)
When I do the simulation this way really right, but when I run on the dataframe, it brings some NA values where I shouldn’t.
– Roney Wesley Galan
I open a new question for this item in question or I can deal directly with this already opened item?
– Roney Wesley Galan
I would open a new question, following the instructions of that link (mainly in the use of function
dput
), so that it is possible to reproduce your actual conditions in the best way.– Marcus Nunes