Description Usage Arguments Details Value Examples
View source: R/MazamaRollUtils.R
Apply a moving-window median function to a numeric vector.
1 2 3 4 5 6 7 | roll_median(
x,
width = 1L,
by = 1L,
align = c("center", "left", "right"),
na.rm = FALSE
)
|
x |
Numeric vector. |
width |
Integer width of the rolling window. |
by |
Integer shift by which the window is moved each iteration. |
align |
Character position of the return value within the window. One of:
|
na.rm |
Logical specifying whether |
For every index in the incoming vector x
, a value is returned that
is the median of all values in x
that fall within a window of width
width
.
The align
parameter determines the alignment of the return value
within the window. Thus:
align = -1 [*------]
will cause the returned vector to have width-1 NA
values at the right end.
align = 0 [---*---]
will cause the returned vector to have width/2 NA
values at either end.
align = 1 [------*]
will cause the returned vector to have width-1 NA
values at the left end.
For large vectors, theby
parameter can be used to force the window
to jump ahead by
indices for the next calculation. Indices that are
skipped over will be assigned NA
values so that the return vector still has
the same length as the incoming vector. This can dramatically speed up
calculations for high resolution time series data.
Numeric vector of the same length as x
.
1 2 3 4 5 6 7 8 9 10 11 12 13 | library(MazamaRollUtils)
# Example air quality time series
t <- example_pm25$datetime
x <- example_pm25$pm25
plot(t, x, pch = 16, cex = 0.5)
lines(t, roll_median(x, width = 3), col = "goldenrod")
lines(t, roll_median(x, width = 23), col = "purple")
legend("topright", lty = c(1, 1),
col = c("goldenrod", "purple"),
legend = c("3-hr median", "12-hr median"))
title("3- and 23-hr Rolling median")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.