| slide2 {tsibble} | R Documentation |
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, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE) slide2_dfr(.x, .y, .f, ..., .size = 1, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE, .id = NULL) slide2_dfc(.x, .y, .f, ..., .size = 1, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE) pslide(.l, .f, ..., .size = 1, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE) pslide_dfr(.l, .f, ..., .size = 1, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE, .id = NULL) pslide_dfc(.l, .f, ..., .size = 1, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE)
.x, .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.
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 |
... |
Additional arguments passed on to |
.size |
An integer for window size. If positive, moving forward from left to right; if negative, moving backward (from right to left). |
.fill |
A value to fill at the left of the data range ( |
.partial |
if |
.align |
Align index at the "right", "centre"/"center", or "left"
of the window. If |
.bind |
If |
.id |
If not |
.l |
A list of lists. The length of |
Other sliding window functions: slide
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)
slide2(lst, lst, ~ ., .size = 2)
slide2(df, df, ~ ., .size = 2)
pslide(lst, ~ ., .size = 1)
pslide(list(lst, lst), ~ ., .size = 2)
###
# 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()
## 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))
# row-oriented workflow
## Not run:
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)))
## End(Not run)