slide_period()
breaks up the .i
ndex by .period
, and then uses that
to define the indices to slide over .x
with.
It can be useful for, say, sliding over daily data in monthly chunks.
The underlying engine for breaking up .i
is warp::warp_distance()
.
If you need more information about the .period
types, that is the best
place to look.
slide_period( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE ) slide_period_vec( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE, .ptype = NULL ) slide_period_dbl( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE ) slide_period_int( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE ) slide_period_lgl( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE ) slide_period_chr( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE ) slide_period_dfr( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE, .names_to = rlang::zap(), .name_repair = c("unique", "universal", "check_unique") ) slide_period_dfc( .x, .i, .period, .f, ..., .every = 1L, .origin = NULL, .before = 0L, .after = 0L, .complete = FALSE, .size = NULL, .name_repair = c("unique", "universal", "check_unique", "minimal") )
.x 
The vector to iterate over and apply 

.i 
A datetime index to break into periods. There are 3 restrictions on the index:

.period 
A string defining the period to group by. Valid inputs can be roughly broken into:

.f 
If a function, it is used as is. If a formula, e.g.
This syntax allows you to create very compact anonymous functions. 
...  Additional arguments passed on to the mapped function. 
.every 
The number of periods to group together. For example, if the period was set to 
.origin 
The reference date time value. The default when left as This is generally used to define the anchor time to count from, which is
relevant when the every value is 
.before, .after 
The number of values before or after the current element to
include in the sliding window. Set to 
.complete 
Should the function be evaluated on complete windows only? If 
.ptype 
A prototype corresponding to the type of the output. If If supplied, the result of each call to If 
.names_to  This controls what to do with input names supplied in

.name_repair  One of With 
.size  If, Alternatively, specify the desired number of rows, and any inputs of length 1 will be recycled appropriately. 
A vector fulfilling the following invariants:
slide_period()
vec_size(slide_period(.x)) == vec_size(unique(warp::warp_distance(.i)))
vec_ptype(slide_period(.x)) == list()
slide_period_vec()
and slide_period_*()
variantsvec_size(slide_period_vec(.x)) == vec_size(unique(warp::warp_distance(.i)))
vec_size(slide_period_vec(.x)[[1]]) == 1L
vec_ptype(slide_period_vec(.x, .ptype = ptype)) == ptype
i < as.Date("20190128") + 0:5 # Split `i` into 2day periods to apply `.f` to slide_period(i, i, "day", identity, .every = 2)#> [[1]] #> [1] "20190128" "20190129" #> #> [[2]] #> [1] "20190130" "20190131" #> #> [[3]] #> [1] "20190201" "20190202" #># Or into 1month periods slide_period(i, i, "month", identity)#> [[1]] #> [1] "20190128" "20190129" "20190130" "20190131" #> #> [[2]] #> [1] "20190201" "20190202" #># Now select: #  The current 2day period #  Plus 1 2day period before the current one slide_period(i, i, "day", identity, .every = 2, .before = 1)#> [[1]] #> [1] "20190128" "20190129" #> #> [[2]] #> [1] "20190128" "20190129" "20190130" "20190131" #> #> [[3]] #> [1] "20190130" "20190131" "20190201" "20190202" #># Alter the `origin` to control the reference date for # how the 2day groups are formed origin < as.Date("20190129") slide_period(i, i, "day", identity, .every = 2, .origin = origin)#> [[1]] #> [1] "20190128" #> #> [[2]] #> [1] "20190129" "20190130" #> #> [[3]] #> [1] "20190131" "20190201" #> #> [[4]] #> [1] "20190202" #># This can be useful for, say, monthly averages daily_sales < c(2, 5, 3, 6, 9, 4) slide_period_dbl(daily_sales, i, "month", mean)#> [1] 4.0 6.5# If you need the index, slide over and return a data frame sales_df < data.frame(i = i, sales = daily_sales) slide_period_dfr( sales_df, sales_df$i, "month", ~data.frame( i = max(.x$i), sales = mean(.x$sales) ) )#> i sales #> 1 20190131 4.0 #> 2 20190202 6.5# One of the most unique features about `slide_period()` is that it is # aware of how far apart elements of `.i` are in the `.period` you are # interested in. For example, if you do a monthly slide with `i2`, selecting # the current month and 1 month before it, then it will recognize that # `20190201` and `20190401` are not beside each other, and it won't # group them together. i2 < as.Date(c("20190101", "20190201", "20190401", "20190501")) slide_period(i2, i2, "month", identity, .before = 1)#> [[1]] #> [1] "20190101" #> #> [[2]] #> [1] "20190101" "20190201" #> #> [[3]] #> [1] "20190401" #> #> [[4]] #> [1] "20190401" "20190501" #>