Running mean in specified window of numeric vector.
numeric vector which running function is calculated on
(integer`` vector or single value)\cr Denoting size of the running window. If kis a single value then window size is constant for all elements, otherwise iflength(k) == length(x)`
different window size for each element.
(integer vector or single value)
Denoting window lag. If lag is a single value then window lag is constant
for all elements, otherwise if length(lag) == length(x) different window
size for each element. Negative value shifts window forward.
(integer, Date, POSIXt)
Optional integer vector containing sorted (ascending) index of observation.
By default idx is index incremented by one. User can provide index with
varying increment and with duplicated values. If specified then k and lag
are depending on idx. Length of idx have to be equal of length x.
(integer, Date, POSIXt, character vector)
Vector of any size and any value defining output data points. Values of the
vector defines the indexes which data is computed at.
logical single value (default na_rm = TRUE) -
if TRUE sum is calculating excluding NA.
(logical single value)
Whether incomplete window should return NA (if na_pad = TRUE)
Incomplete window is when some parts of the window are out of range.
mean (numeric) vector of length equals length of x.
set.seed(11)
x1 <- rnorm(15)
x2 <- sample(c(rep(NA, 5), rnorm(15)), 15, replace = TRUE)
k <- sample(1:15, 15, replace = TRUE)
mean_run(x1)
#> [1] -0.5910311 -0.2822184 -0.6936633 -0.8609108 -0.4530308 -0.5332176
#> [7] -0.2679571 -0.1563477 -0.1440561 -0.2300625 -0.2844599 -0.2897842
#> [13] -0.3858234 -0.3765192 -0.4280809
mean_run(x2, na_rm = TRUE)
#> [1] -0.18760011 -0.09022066 -0.06543317 0.03906450 -0.12188853 -0.13873536
#> [7] -0.13873536 -0.14571604 -0.12596067 -0.11116961 -0.09881996 -0.08871569
#> [13] -0.05194292 -0.04699909 -0.05704202
mean_run(x2, na_rm = FALSE)
#> [1] -0.18760011 -0.09022066 -0.06543317 0.03906450 -0.12188853 -0.13873536
#> [7] NA NA NA NA NA NA
#> [13] NA NA NA
mean_run(x2, na_rm = TRUE, k = 4)
#> [1] -0.18760011 -0.09022066 -0.06543317 0.03906450 -0.10546063 -0.16299272
#> [7] -0.21203756 -0.39209010 -0.13274756 -0.05603811 -0.03894684 0.01103493
#> [13] 0.09609256 0.09738460 0.04740283