Fixing an initial window and expanding more observations:

  • stretch() always returns a vector of numerics

  • stretch_lst() returns a list

  • stretch_dfr() return data frame using row-binding

  • stretcher() splits the input x to a list according to the window size.

stretch(x, .f, ..., size = 1, init = 1)

stretch_lst(x, .f, ..., size = 1, init = 1)

stretch_dfr(x, .f, ..., size = 1, init = 1, .id = NULL)

stretcher(x, size = 1, init = 1)

Arguments

x

A vector of numerics, or data frame. If a data frame, row-wise rolling window is performed.

.f

A function or one-sided formula using purrr-like syntax. If a formula, it is converted to a function.

...

Additional arguments passed on to .f.

size, init

An integer for moving and initial window size.

.id

If not NULL a variable with this name will be created giving either the name or the index of the data frame, which is passed to dplyr::bind_rows.

See also

slide for sliding window with overlapping observations; tile for tiling window without overlapping observations.

Examples

x <- 1:10 stretch(x, mean, init = 3)
#> [1] 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
stretch(x, ~ mean(.), init = 3)
#> [1] 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
stretcher(x, init = 3)
#> [[1]] #> [1] 1 2 3 #> #> [[2]] #> [1] 1 2 3 4 #> #> [[3]] #> [1] 1 2 3 4 5 #> #> [[4]] #> [1] 1 2 3 4 5 6 #> #> [[5]] #> [1] 1 2 3 4 5 6 7 #> #> [[6]] #> [1] 1 2 3 4 5 6 7 8 #> #> [[7]] #> [1] 1 2 3 4 5 6 7 8 9 #> #> [[8]] #> [1] 1 2 3 4 5 6 7 8 9 10 #>
# stretching a 2-day window for a data frame ---- sx <- pedestrian %>% filter(Sensor == "Southern Cross Station", Date <= as.Date("2015-01-10")) sx %>% stretch_dfr(~ quantile(.$Count), init = 48)
#> # A tibble: 193 x 5 #> `0%` `25%` `50%` `75%` `100%` #> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 6 38.8 102. 229. 746 #> 2 6 38 98 215 746 #> 3 6 36.5 98 206. 746 #> 4 6 36 98 198. 746 #> 5 6 33.5 96.5 189. 746 #> 6 6 26 95 180 746 #> 7 6 24.5 94 176 746 #> 8 6 22.5 93 172 746 #> 9 6 23.2 90 168 746 #> 10 6 24 87 164 746 #> # ... with 183 more rows