Rolling window with overlapping observations:

  • slide() always returns a vector of numerics

  • slide_lst() returns a list

  • slide_dfr() return data frame using row-binding

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

slide(x, .f, ..., size = 1, fill = NA_real_)

slide_lst(x, .f, ..., size = 1, fill = NA)

slide_dfr(x, .f, ..., size = 1, fill = NA, .id = NULL)

slider(x, size = 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

An integer for window size.

fill

A single value or data frame to replace NA.

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

Details

The slide() function attempts to tackle more general problems using the purrr-like syntax. For some specialist functions like mean and sum, you may like to check out for RcppRoll for faster performance.

See also

tile for tiling window without overlapping observations; stretch for expanding more observations

Examples

# sliding through a vector ---- x <- 1:10 slide(x, mean, size = 3)
#> [1] NA NA 2 3 4 5 6 7 8 9
slide(x, ~ mean(.), size = 3)
#> [1] NA NA 2 3 4 5 6 7 8 9
slide(x, mean, size = 3, fill = 0)
#> [1] 0 0 2 3 4 5 6 7 8 9
# slider ---- slider(x, size = 3)
#> [[1]] #> [1] 1 2 3 #> #> [[2]] #> [1] 2 3 4 #> #> [[3]] #> [1] 3 4 5 #> #> [[4]] #> [1] 4 5 6 #> #> [[5]] #> [1] 5 6 7 #> #> [[6]] #> [1] 6 7 8 #> #> [[7]] #> [1] 7 8 9 #> #> [[8]] #> [1] 8 9 10 #>
# takes a little longer for cran check # sliding a 2-day window for a data frame ---- jan <- pedestrian %>% filter(Date <= as.Date("2015-01-31")) %>% split_by(Sensor) # returns a data frame of fitted values and residuals for each sensor, # and then combines diag_jan <- jan %>% purrr::map_dfr( ~ slide_dfr(., function(x) { fit <- lm(Count ~ Time, data = x) data.frame(fitted = fitted(fit), resid = residuals(fit)) }, size = 48) ) diag_jan[48:57, ]
#> # A tibble: 10 x 2 #> fitted resid #> <dbl> <dbl> #> 1 337. 1293. #> 2 331. 495. #> 3 325. 242. #> 4 319. -55.3 #> 5 313. -174. #> 6 308. -231. #> 7 302. -258. #> 8 296. -240. #> 9 290. -177. #> 10 284. -118.
# save lm models as additional columns lm_jan <- jan %>% purrr::map( ~ mutate(., lm = slide_lst(., ~ lm(Count ~ Time, data = .), size = 48) )) lm_jan[[1]][48:57, ]
#> # A tsibble: 10 x 6 [1HOUR] #> # Keys: Sensor [1] #> Sensor Date_Time Date Time Count lm #> <chr> <dttm> <date> <int> <int> <list> #> 1 Birrarung Marr 2015-01-02 23:00:00 2015-01-02 23 64 <S3: lm> #> 2 Birrarung Marr 2015-01-03 00:00:00 2015-01-03 0 54 <S3: lm> #> 3 Birrarung Marr 2015-01-03 01:00:00 2015-01-03 1 48 <S3: lm> #> 4 Birrarung Marr 2015-01-03 02:00:00 2015-01-03 2 24 <S3: lm> #> 5 Birrarung Marr 2015-01-03 03:00:00 2015-01-03 3 17 <S3: lm> #> 6 Birrarung Marr 2015-01-03 04:00:00 2015-01-03 4 3 <S3: lm> #> 7 Birrarung Marr 2015-01-03 05:00:00 2015-01-03 5 17 <S3: lm> #> 8 Birrarung Marr 2015-01-03 06:00:00 2015-01-03 6 82 <S3: lm> #> 9 Birrarung Marr 2015-01-03 07:00:00 2015-01-03 7 135 <S3: lm> #> 10 Birrarung Marr 2015-01-03 08:00:00 2015-01-03 8 121 <S3: lm>