stretch() is an S3 method to carry out expanding window calculation; stretcher() splits the input x to a list according to the window size.

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

# S3 method for default
stretch(x, .f, ..., size = 1, init = 1)

# S3 method for data.frame
stretch(x, .f, ..., size = 1, init = 1,
  deframe = TRUE)

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

Arguments

x

A vector of numerics, or data frame.

.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.

deframe

TRUE a list is returned. FALSE returns a data frame.

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(~ quantile(.$Count), init = 48, deframe = FALSE)
#> # A tibble: 193 x 5 #> `0%` `25%` `50%` `75%` `100%` #> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 6.00 38.8 102 229 746 #> 2 6.00 38.0 98.0 215 746 #> 3 6.00 36.5 98.0 206 746 #> 4 6.00 36.0 98.0 198 746 #> 5 6.00 33.5 96.5 189 746 #> 6 6.00 26.0 95.0 180 746 #> 7 6.00 24.5 94.0 176 746 #> 8 6.00 22.5 93.0 172 746 #> 9 6.00 23.2 90.0 168 746 #> 10 6.00 24.0 87.0 164 746 #> # ... with 183 more rows