epi_slide | R Documentation |
epi_df
objectSlides a given function over variables in an epi_df
object.
This is useful for computations like rolling averages. The function supports
many ways to specify the computation, but by far the most common use case is
as follows:
# Create new column `cases_7dm` that contains a 7-day trailing median of cases epi_slide(edf, cases_7dav = median(cases), .window_size = 7)
For two very common use cases, we provide optimized functions that are much
faster than epi_slide
: epi_slide_mean()
and epi_slide_sum()
. We
recommend using these functions when possible.
See vignette("epi_df")
for more examples.
epi_slide(
.x,
.f,
...,
.window_size = NULL,
.align = c("right", "center", "left"),
.ref_time_values = NULL,
.new_col_name = NULL,
.all_rows = FALSE
)
.x |
An |
.f |
Function, formula, or missing; together with
|
... |
Additional arguments to pass to the function or formula specified
via |
.window_size |
The size of the sliding window. The accepted values
depend on the type of the
|
.align |
The alignment of the sliding window.
|
.ref_time_values |
The time values at which to compute the slides
values. By default, this is all the unique time values in |
.new_col_name |
Name for the new column that will contain the computed
values. The default is "slide_value" unless your slide computations output
data frames, in which case they will be unpacked (as in |
.all_rows |
If |
.f
via tidy evaluationIf specifying .f
via tidy evaluation, in addition to the standard .data
and .env
, we make some additional "pronoun"-like bindings available:
.x, which is like .x
in dplyr::group_modify
; an ordinary object
like an epi_df
rather than an rlang pronoun
like .data
; this allows you to use additional dplyr
, tidyr
, and
epiprocess
operations. If you have multiple expressions in ...
, this
won't let you refer to the output of the earlier expressions, but .data
will.
.group_key, which is like .y
in dplyr::group_modify
.
.ref_time_value, which is the element of .ref_time_values
that
determined the time window for the current computation.
An epi_df
object with one or more new slide computation columns
added.
epi_slide_opt
for optimized slide functions
# Get the 7-day trailing standard deviation of cases and the 7-day trailing mean of cases
cases_deaths_subset %>%
epi_slide(
cases_7sd = sd(cases, na.rm = TRUE),
cases_7dav = mean(cases, na.rm = TRUE),
.window_size = 7
) %>%
dplyr::select(geo_value, time_value, cases, cases_7sd, cases_7dav)
# The same as above, but unpacking using an unnamed data.frame with a formula
cases_deaths_subset %>%
epi_slide(
~ data.frame(
cases_7sd = sd(.x$cases, na.rm = TRUE),
cases_7dav = mean(.x$cases, na.rm = TRUE)
),
.window_size = 7
) %>%
dplyr::select(geo_value, time_value, cases, cases_7sd, cases_7dav)
# The same as above, but packing using a named data.frame with a tidy evaluation
# expression
cases_deaths_subset %>%
epi_slide(
slide_packed = data.frame(
cases_7sd = sd(.x$cases, na.rm = TRUE),
cases_7dav = mean(.x$cases, na.rm = TRUE)
),
.window_size = 7
) %>%
dplyr::select(geo_value, time_value, cases, slide_packed)
# nested new columns
cases_deaths_subset %>%
group_by(geo_value) %>%
epi_slide(
function(x, g, t) {
data.frame(
cases_7sd = sd(x$cases, na.rm = TRUE),
cases_7dav = mean(x$cases, na.rm = TRUE)
)
},
.window_size = 7
) %>%
dplyr::select(geo_value, time_value, cases, cases_7sd, cases_7dav)
# Use the geo_value or the ref_time_value in the slide computation
cases_deaths_subset %>%
epi_slide(~ .x$geo_value[[1]], .window_size = 7)
cases_deaths_subset %>%
epi_slide(~ .x$time_value[[1]], .window_size = 7)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.