slider 行
library(slider) library(lubridate) library(tidyverse) x <- c(1, 2, 3, 4, 5) # .before: How many elements before the current one should be included in the window? # .after: How many elements after the current one should be included in the window? # .complete: Should .f only be evaluated when there is enough data to make a complete window?ff # .step: The number of elements to shift forward between calls to .f. slide_vec(x, mean, .before = 1) slide_vec(x, mean, .after = 1) slide_vec(x, sum, .before = 2) slide_vec(x, sum, .before = 2, .complete = T) index_vec <- as.Date("2019-08-29") + c(0, 1, 5, 6) wday_vec <- as.character(wday(index_vec, label = TRUE)) sales_vec <- c(2, 4, 3, 5) company <- tibble(sales = sales_vec, index = index_vec, wday = wday_vec) # Over columns: map(company, ~ .x) # Over rows: # slide(company, ~ .x) big_index_vec <- c(as.Date("2019-08-30") + 0:4,