Rolling window with overlapping observations:

  • slide2() and pslide() always returns a list.

  • slide2_lgl(), slide2_int(), slide2_dbl(), slide2_chr() use the same arguments as slide2(), but return vectors of the corresponding type.

  • slide2_dfr() slide2_dfc() return data frames using row-binding & column-binding.

slide2(.x, .y, .f, ..., .size = 1, .step = 1, .fill = NA,
  .partial = FALSE, .align = "right", .bind = FALSE)

slide2_dfr(.x, .y, .f, ..., .size = 1, .step = 1, .fill = NA,
  .partial = FALSE, .align = "right", .bind = FALSE, .id = NULL)

slide2_dfc(.x, .y, .f, ..., .size = 1, .step = 1, .fill = NA,
  .partial = FALSE, .align = "right", .bind = FALSE)

pslide(.l, .f, ..., .size = 1, .step = 1, .fill = NA,
  .partial = FALSE, .align = "right", .bind = FALSE)

pslide_dfr(.l, .f, ..., .size = 1, .step = 1, .fill = NA,
  .partial = FALSE, .align = "right", .bind = FALSE, .id = NULL)

pslide_dfc(.l, .f, ..., .size = 1, .step = 1, .fill = NA,
  .partial = FALSE, .align = "right", .bind = FALSE)

Arguments

.x, .y

Objects to slide over simultaneously.

.f

A function, formula, or vector (not necessarily atomic).

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. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to the mapped function.

.size

An integer for window size. If positive, moving forward from left to right; if negative, moving backward (from right to left).

.step

A positive integer for calculating at every specified step instead of every single step.

.fill

A value to fill at the left/center/right of the data range depending on .align (NA by default). NULL means no filling.

.partial

if TRUE, partial sliding.

.align

Align index at the "right", "centre"/"center", or "left" of the window. If .size is even for center alignment, "centre-right" & "centre-left" is needed.

.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

Either a string or NULL. If a string, the output will contain a variable with that name, storing either the name (if .x is named) or the index (if .x is unnamed) of the input. If NULL, the default, no variable will be created.

Only applies to _dfr variant.

.l

A list of vectors, such as a data frame. The length of .l determines the number of arguments that .f will be called with. List names will be used if present.

See also

  • tile2 for tiling window without overlapping observations

  • stretch2 for expanding more observations

Other sliding window functions: slide

Examples

x <- 1:5 y <- 6:10 z <- 11:15 lst <- list(x = x, y = y, z = z) df <- as.data.frame(lst) slide2(x, y, sum, .size = 2)
#> [[1]] #> [1] NA #> #> [[2]] #> [1] 16 #> #> [[3]] #> [1] 20 #> #> [[4]] #> [1] 24 #> #> [[5]] #> [1] 28 #>
slide2(lst, lst, ~ ., .size = 2)
#> [[1]] #> [1] NA #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> #> [[3]] #> [[3]]$y #> [1] 6 7 8 9 10 #> #> [[3]]$z #> [1] 11 12 13 14 15 #> #>
slide2(df, df, ~ ., .size = 2)
#> [[1]] #> [1] NA #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> #> [[3]] #> [[3]]$y #> [1] 6 7 8 9 10 #> #> [[3]]$z #> [1] 11 12 13 14 15 #> #>
pslide(lst, ~ ., .size = 1)
#> [[1]] #> [1] 1 #> #> [[2]] #> [1] 2 #> #> [[3]] #> [1] 3 #> #> [[4]] #> [1] 4 #> #> [[5]] #> [1] 5 #>
pslide(list(lst, lst), ~ ., .size = 2)
#> [[1]] #> [1] NA #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> #> [[3]] #> [[3]]$y #> [1] 6 7 8 9 10 #> #> [[3]]$z #> [1] 11 12 13 14 15 #> #>
### # row-wise sliding over data frame ### my_df <- data.frame( group = rep(letters[1:2], each = 8), x = c(1:8, 8:1), y = 2 * c(1:8, 8:1) + rnorm(16), date = rep(as.Date("2016-06-01") + 0:7, 2) ) slope <- function(...) { data <- list(...) fm <- lm(y ~ x, data = data) coef(fm)[[2]] } my_df %>% nest(-group) %>% mutate(slope = purrr::map(data, ~ pslide_dbl(., slope, .size = 2))) %>% unnest()
#> # A tibble: 16 x 5 #> group slope x y date #> <fct> <dbl> <int> <dbl> <date> #> 1 a NA 1 1.30 2016-06-01 #> 2 a 2.71 2 4.01 2016-06-02 #> 3 a 4.03 3 8.03 2016-06-03 #> 4 a -1.38 4 6.66 2016-06-04 #> 5 a 4.50 5 11.2 2016-06-05 #> 6 a 0.638 6 11.8 2016-06-06 #> 7 a 1.83 7 13.6 2016-06-07 #> 8 a 4.11 8 17.7 2016-06-08 #> 9 b NA 8 15.2 2016-06-01 #> 10 b 2.12 7 13.0 2016-06-02 #> 11 b 0.0209 6 13.0 2016-06-03 #> 12 b 4.51 5 8.50 2016-06-04 #> 13 b 1.69 4 6.82 2016-06-05 #> 14 b 0.185 3 6.63 2016-06-06 #> 15 b 0.529 2 6.10 2016-06-07 #> 16 b 4.71 1 1.39 2016-06-08
## window over 2 months pedestrian %>% filter(Sensor == "Southern Cross Station") %>% index_by(yrmth = yearmonth(Date_Time)) %>% nest(-yrmth) %>% mutate(ma = slide_dbl(data, ~ mean(.$Count), .size = 2, .bind = TRUE))
#> # A tibble: 24 x 3 #> yrmth data ma #> <mth> <list> <dbl> #> 1 2015 Jan <tsibble [744 × 5]> NA #> 2 2015 Feb <tsibble [672 × 5]> 424. #> 3 2015 Mar <tsibble [744 × 5]> 477. #> 4 2015 Apr <tsibble [720 × 5]> 459. #> 5 2015 May <tsibble [744 × 5]> 463. #> 6 2015 Jun <tsibble [720 × 5]> 489. #> 7 2015 Jul <tsibble [744 × 5]> 513. #> 8 2015 Aug <tsibble [744 × 5]> 508. #> 9 2015 Sep <tsibble [720 × 5]> 500. #> 10 2015 Oct <tsibble [743 × 5]> 508. #> # … with 14 more rows
# row-oriented workflow
my_diag <- function(...) { data <- list(...) fit <- lm(Count ~ Time, data = data) tibble(fitted = fitted(fit), resid = residuals(fit)) } pedestrian %>% filter_index("2015-01") %>% nest(-Sensor) %>% mutate(diag = purrr::map(data, ~ pslide_dfr(., my_diag, .size = 48)))
#> # A tibble: 3 x 3 #> Sensor data diag #> <chr> <list> <list> #> 1 Birrarung Marr <tsibble [744 × 4]> <tibble [33,457 × 2]> #> 2 QV Market-Elizabeth St (West) <tsibble [744 × 4]> <tibble [33,457 × 2]> #> 3 Southern Cross Station <tsibble [744 × 4]> <tibble [33,457 × 2]>