Fixing an initial window and expanding more observations:

  • stretch2() and pstretch() always returns a list.

  • stretch2_lgl(), stretch2_int(), stretch2_dbl(), stretch2_chr() use the same arguments as stretch2(), but return vectors of the corresponding type.

  • stretch2_dfr() stretch2_dfc() return data frames using row-binding & column-binding.

stretch2(.x, .y, .f, ..., .size = 1, .init = 1, .bind = FALSE)

stretch2_dfr(.x, .y, .f, ..., .size = 1, .init = 1, .bind = FALSE,
  .id = NULL)

stretch2_dfc(.x, .y, .f, ..., .size = 1, .init = 1, .bind = FALSE)

pstretch(.l, .f, ..., .size = 1, .init = 1, .bind = FALSE)

pstretch_dfr(.l, .f, ..., .size = 1, .init = 1, .bind = FALSE,
  .id = NULL)

pstretch_dfc(.l, .f, ..., .size = 1, .init = 1, .bind = FALSE)

Arguments

.x

Objects to slide over simultaneously.

.y

Objects to slide over simultaneously.

.f

A function, formula, or atomic vector.

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. Within a list, wrap strings in get-attr() to extract named attributes. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to .f.

.size, .init

An integer for moving and initial window size.

.bind

If .x is a list, should .x be combined before applying .f? If .x is a list of data frames, row binding is carried out.

.id

If not NULL a variable with this name will be created giving either the name or the index of the data frame.

.l

A list of lists. The length of .l determines the number of arguments that .f will be called with. List names will be used if present.

See also

  • slide2 for sliding window with overlapping observations

  • tile2 for tiling window without overlapping observations

Other stretching window functions: stretch

Examples

x <- 1:5 y <- 6:10 z <- 11:15 lst <- list(x = x, y = y, z = z) df <- as.data.frame(lst) stretch2(x, y, sum, .size = 2)
#> [[1]] #> [1] 7 #> #> [[2]] #> [1] 27 #> #> [[3]] #> [1] 55 #>
stretch2(lst, lst, ~ ., .size = 2)
#> [[1]] #> [[1]]$x #> [1] 1 2 3 4 5 #> #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> [[2]]$z #> [1] 11 12 13 14 15 #> #>
stretch2(df, df, ~ ., .size = 2)
#> [[1]] #> [[1]]$x #> [1] 1 2 3 4 5 #> #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> [[2]]$z #> [1] 11 12 13 14 15 #> #>
pstretch(lst, sum, .size = 1)
#> [[1]] #> [1] 18 #> #> [[2]] #> [1] 39 #> #> [[3]] #> [1] 63 #> #> [[4]] #> [1] 90 #> #> [[5]] #> [1] 120 #>
pstretch(list(lst, lst), ~ ., .size = 2)
#> [[1]] #> [[1]]$x #> [1] 1 2 3 4 5 #> #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> [[2]]$z #> [1] 11 12 13 14 15 #> #>
### # row-wise stretching over data frame ### x <- as.Date("2017-01-01") + 0:364 df <- data.frame(x = x, y = seq_along(x)) tibble( data = pstretch(df, function(...) as_tibble(list(...)), .init = 10) )
#> # A tibble: 356 x 1 #> data #> <list> #> 1 <tibble [10 × 2]> #> 2 <tibble [11 × 2]> #> 3 <tibble [12 × 2]> #> 4 <tibble [13 × 2]> #> 5 <tibble [14 × 2]> #> 6 <tibble [15 × 2]> #> 7 <tibble [16 × 2]> #> 8 <tibble [17 × 2]> #> 9 <tibble [18 × 2]> #> 10 <tibble [19 × 2]> #> # … with 346 more rows