diff --git a/R/hop-index.R b/R/hop-index.R index eccd7b9..b17aa4b 100644 --- a/R/hop-index.R +++ b/R/hop-index.R @@ -63,7 +63,7 @@ #' hop_index(i, i, starts, stops, ~.x) #' #' # --------------------------------------------------------------------------- -#' # Non-existant dates with `lubridate::months()` +#' # Non-existent dates with `lubridate::months()` #' #' # Imagine you want to compute a 1 month rolling average on this #' # irregular daily data. @@ -81,7 +81,7 @@ #' ) #' #' # This is because when you actually compute the `.i - .before` sequence, -#' # you hit non-existant dates. i.e. `"2019-03-29" - months(1)` doesn't exist. +#' # you hit non-existent dates. i.e. `"2019-03-29" - months(1)` doesn't exist. #' i - months(1) #' #' # To get around this, lubridate provides `add_with_rollback()`, diff --git a/R/slide-index2.R b/R/slide-index2.R index 2a94d45..d63ec54 100644 --- a/R/slide-index2.R +++ b/R/slide-index2.R @@ -1,4 +1,4 @@ -#' Slide along multiples inputs simultaneously relative to an index +#' Slide along multiple inputs simultaneously relative to an index #' #' `slide_index2()` and `pslide_index()` represent the combination #' of [slide2()] and [pslide()] with [slide_index()], allowing you to iterate diff --git a/R/summary-index.R b/R/summary-index.R index c59368a..fe3ba76 100644 --- a/R/summary-index.R +++ b/R/summary-index.R @@ -63,6 +63,30 @@ #' #' # Only evaluate the sum on windows that have the potential to be complete #' slide_index_sum(x, i, before = 2, after = 1, complete = TRUE) +#' +#' # --------------------------------------------------------------------------- +#' # Panel data (grouped rolling operations) +#' +#' # In Stata, the equivalent of grouped rolling means is: +#' # rangestat (mean) roll_mean = sales, int(date -2 0) by(firm) +#' +#' # slider + dplyr replicates this pattern with group_by(): +#' df <- data.frame( +#' firm = rep(c("A", "B"), each = 4), +#' date = as.Date(c( +#' "2020-01-01", "2020-01-02", "2020-01-05", "2020-01-06", +#' "2020-01-01", "2020-01-03", "2020-01-04", "2020-01-07" +#' )), +#' sales = c(10, 12, 15, 13, 20, 22, 18, 25) +#' ) +#' +#' # 3-day rolling mean within each firm, respecting irregular date gaps +#' df$roll_mean <- unlist( +#' lapply(split(df, df$firm), function(g) { +#' slide_index_mean(g$sales, g$date, before = 2) +#' }) +#' ) +#' df slide_index_sum <- function( x, i, diff --git a/man/hop_index.Rd b/man/hop_index.Rd index 39e503a..8890ff3 100644 --- a/man/hop_index.Rd +++ b/man/hop_index.Rd @@ -125,7 +125,7 @@ stops <- vec_c(inner - 1, last_stop) hop_index(i, i, starts, stops, ~.x) # --------------------------------------------------------------------------- -# Non-existant dates with `lubridate::months()` +# Non-existent dates with `lubridate::months()` # Imagine you want to compute a 1 month rolling average on this # irregular daily data. @@ -143,7 +143,7 @@ with_options( ) # This is because when you actually compute the `.i - .before` sequence, -# you hit non-existant dates. i.e. `"2019-03-29" - months(1)` doesn't exist. +# you hit non-existent dates. i.e. `"2019-03-29" - months(1)` doesn't exist. i - months(1) # To get around this, lubridate provides `add_with_rollback()`, diff --git a/man/slide_index2.Rd b/man/slide_index2.Rd index 7eb5c45..82088d3 100644 --- a/man/slide_index2.Rd +++ b/man/slide_index2.Rd @@ -17,7 +17,7 @@ \alias{pslide_index_chr} \alias{pslide_index_dfr} \alias{pslide_index_dfc} -\title{Slide along multiples inputs simultaneously relative to an index} +\title{Slide along multiple inputs simultaneously relative to an index} \usage{ slide_index2(.x, .y, .i, .f, ..., .before = 0L, .after = 0L, .complete = FALSE) diff --git a/man/summary-index.Rd b/man/summary-index.Rd index e1ed27a..7d4af46 100644 --- a/man/summary-index.Rd +++ b/man/summary-index.Rd @@ -183,6 +183,30 @@ slide_index_mean(x, i, before = 2) # Only evaluate the sum on windows that have the potential to be complete slide_index_sum(x, i, before = 2, after = 1, complete = TRUE) + +# --------------------------------------------------------------------------- +# Panel data (grouped rolling operations) + +# In Stata, the equivalent of grouped rolling means is: +# rangestat (mean) roll_mean = sales, int(date -2 0) by(firm) + +# slider + dplyr replicates this pattern with group_by(): +df <- data.frame( + firm = rep(c("A", "B"), each = 4), + date = as.Date(c( + "2020-01-01", "2020-01-02", "2020-01-05", "2020-01-06", + "2020-01-01", "2020-01-03", "2020-01-04", "2020-01-07" + )), + sales = c(10, 12, 15, 13, 20, 22, 18, 25) +) + +# 3-day rolling mean within each firm, respecting irregular date gaps +df$roll_mean <- unlist( + lapply(split(df, df$firm), function(g) { + slide_index_mean(g$sales, g$date, before = 2) + }) +) +df } \seealso{ \code{\link[=slide_sum]{slide_sum()}}