movingSum | R Documentation |
Calculate moving sum of n values "around" values
movingSum(x, n, na.rm = FALSE)
x |
vector of values of which moving sum is to be calculated |
n |
number of values "around" the values in |
na.rm |
logical. Should missing values (including NaN) be omitted from the calculations? |
Vector of moving sums with the same number of values as there are in
x
. If na.rm
is FALSE
, the first (n-1)/2
values
and the last (n-1)/2
values are NA since there are not enough values
at the start and at the end of the vector, respectively, to calculate the
sum.
x <- rnorm(30)
plot(x, type = "b", main = "Moving mean over 3, 5, 7 points")
times <- 2:4
for (i in times) {
lines(movingSum(x, n = 2 * i - 1), col = i, type = "b", lwd = 2)
}
legend("topright", fill = times, legend = sprintf("n = %d", 2 * times - 1))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.