Specialized sliding functions relative to an index
These functions are specialized variants of the most common ways that
slide_index()
is generally used. Notably, slide_index_sum()
can be used
for rolling sums relative to an index (like a Date column), and
slide_index_mean()
can be used for rolling averages.
These specialized variants are much faster and more memory efficient than
using an otherwise equivalent call constructed with slide_index_dbl()
or slide_index_lgl()
, especially with a very wide window.
slide_index_sum( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE ) slide_index_prod( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE ) slide_index_mean( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE ) slide_index_min( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE ) slide_index_max( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE ) slide_index_all( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE ) slide_index_any( x, i, ..., before = 0L, after = 0L, complete = FALSE, na_rm = FALSE )
x |
A vector to compute the sliding function on.
|
i |
The index vector that determines the window sizes. It is fairly common to supply a date vector as the index, but not required. There are 3 restrictions on the index:
|
... |
These dots are for future extensions and must be empty. |
before |
The ranges that result from applying |
after |
The ranges that result from applying |
complete |
Should the function be evaluated on complete windows only? If |
na_rm |
Should missing values be removed from the computation? |
For more details about the implementation, see the help page of
slide_sum()
.
A vector the same size as x
containing the result of applying the
summary function over the sliding windows.
For sliding sum, mean, prod, min, and max, a double vector will be returned.
For sliding any and all, a logical vector will be returned.
x <- c(1, 5, 3, 2, 6, 10) i <- as.Date("2019-01-01") + c(0, 1, 3, 4, 6, 8) # `slide_index_sum()` can be used for rolling sums relative to an index, # allowing you to "respect gaps" in your series. Notice that the rolling # sum in row 3 is only computed from `2019-01-04` and `2019-01-02` since # `2019-01-01` is more than two days before the current date. data.frame( i = i, x = x, roll = slide_index_sum(x, i, before = 2) ) # `slide_index_mean()` can be used for rolling averages slide_index_mean(x, i, before = 2) # Only evaluate the sum on windows that have the potential to be complete slide_index_sum(x, i, before = 2, after = 1, complete = TRUE)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.