epi_slide_opt | R Documentation |
epi_slide_opt
allows sliding an n-timestep data.table::froll
or slider::summary-slide function over variables in an epi_df
object.
These functions tend to be much faster than epi_slide()
. See
vignette("epi_df")
for more examples.
epi_slide_mean
is a wrapper around epi_slide_opt
with .f = data.table::frollmean
.
epi_slide_sum
is a wrapper around epi_slide_opt
with .f = data.table::frollsum
.
epi_slide_opt(
.x,
.col_names,
.f,
...,
.window_size = NULL,
.align = c("right", "center", "left"),
.prefix = NULL,
.suffix = NULL,
.new_col_names = NULL,
.ref_time_values = NULL,
.all_rows = FALSE
)
epi_slide_mean(
.x,
.col_names,
...,
.window_size = NULL,
.align = c("right", "center", "left"),
.prefix = NULL,
.suffix = NULL,
.new_col_names = NULL,
.ref_time_values = NULL,
.all_rows = FALSE
)
epi_slide_sum(
.x,
.col_names,
...,
.window_size = NULL,
.align = c("right", "center", "left"),
.prefix = NULL,
.suffix = NULL,
.new_col_names = NULL,
.ref_time_values = NULL,
.all_rows = FALSE
)
.x |
An |
.col_names |
< The tidy-selection renaming interface is not supported, and cannot be used
to provide output column names; if you want to customize the output column
names, use |
.f |
Function; together with The optimized |
... |
Additional arguments to pass to the slide computation |
.window_size |
The size of the sliding window. The accepted values
depend on the type of the
|
.align |
The alignment of the sliding window.
|
.prefix |
Optional |
.suffix |
Optional |
.new_col_names |
Optional character vector with length matching the
number of input columns from |
.ref_time_values |
The time values at which to compute the slides
values. By default, this is all the unique time values in |
.all_rows |
If |
An epi_df
object with one or more new slide computation columns
added. It will be ungrouped if .x
was ungrouped, and have the same groups
as .x
if .x
was grouped.
glue::glue
format strings specially interpret content within curly
braces. E.g., glue::glue("ABC{2 + 2}")
evaluates to "ABC4"
. For .prefix
and .suffix
, we provide glue
with some additional variable bindings:
{.n}
will be the number of time steps in the computation
corresponding to the .window_size
.
{.time_unit_abbr}
will be a lower-case letter corresponding to the
time_type
of .x
{.align_abbr}
will be ""
if .align
is the default of "right"
;
otherwise, it will be the first letter of .align
{.f_abbr}
will be a character vector containing a short abbreviation
for .f
factoring in the input column type(s) for .col_names
epi_slide
for the more general slide function
library(dplyr)
# Add a column (`cases_7dsum`) containing a 7-day trailing sum on `cases`:
cases_deaths_subset %>%
select(geo_value, time_value, cases) %>%
epi_slide_sum(cases, .window_size = 7)
# Add a column (`cases_rate_7dav`) containing a 7-day trailing average on `case_rate`:
covid_case_death_rates_extended %>%
epi_slide_mean(case_rate, .window_size = 7)
# Use a less common specialized slide function:
cases_deaths_subset %>%
epi_slide_opt(cases, slider::slide_min, .window_size = 7)
# Specify output column names and/or a naming scheme:
cases_deaths_subset %>%
select(geo_value, time_value, cases) %>%
group_by(geo_value) %>%
epi_slide_sum(cases, .window_size = 7, .new_col_names = "case_sum") %>%
ungroup()
cases_deaths_subset %>%
select(geo_value, time_value, cases) %>%
group_by(geo_value) %>%
epi_slide_sum(cases, .window_size = 7, .prefix = "sum_") %>%
ungroup()
# Additional settings can be sent to the {data.table} and {slider} functions
# via `...`. This example passes some arguments to `frollmean` settings for
# speed, accuracy, and to allow partially-missing windows:
covid_case_death_rates_extended %>%
epi_slide_mean(
case_rate,
.window_size = 7,
na.rm = TRUE, algo = "exact", hasNA = TRUE
)
# If the more specialized possibilities for `.f` don't cover your needs, you
# can use `epi_slide_opt` with `.f = data.table::frollapply` to apply a
# custom function at the cost of more computation time. See also `epi_slide`
# if you need something even more general.
cases_deaths_subset %>%
select(geo_value, time_value, case_rate_7d_av, death_rate_7d_av) %>%
epi_slide_opt(c(case_rate_7d_av, death_rate_7d_av),
data.table::frollapply,
FUN = median, .window_size = 28,
.suffix = "_{.n}{.time_unit_abbr}_median"
) %>%
print(n = 40)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.