Skip to contents

Common utility functions

Usage

gen_data(years)

Arguments

years

sequence of years, e.g. 2010:2020

Value

tibble() or vector

tibble

Examples

if (FALSE) {
ex <- gen_data(2020:2025)
head(ex)

# Lagged absolute/percentage change,
# rate of return and cumulative sum
dplyr::filter(ex, group == "A") |>
change(pay)

# Geometric mean
ex |>
dplyr::filter(group == "A") |>
ror(pay) |>
dplyr::summarise(gmean = geomean(pay_ror))

# When performing a `group_by()`, watch for
# the correct order of the variables
ex |>
dplyr::group_by(group) |>
change(pay)

ex |>
dplyr::group_by(group) |>
change(pay) |>
dplyr::summarise(mean_pay = mean(pay, na.rm = TRUE),
                 csm_chg  = sum(pay_chg),
                 csm_pct  = sum(pay_pct),
                 mean_ror = mean(pay_ror, na.rm = TRUE),
                 geomean  = geomean(pay_ror))

# Timespans
dt <- dplyr::tibble(date = lubridate::today() - 366)

# `years_df()`
years_df(dt, date)

# `duration_vec()`
dplyr::mutate(dt, dur = duration_vec(date))

# `make_interval()`
dplyr::tibble(date = lubridate::today() - 1000) |>
make_interval(start = date, end = lubridate::today() - 500)


# `summary_stats()`
sm <- dplyr::tibble(provider = sample(c("A", "B", "C"), size = 200, replace = TRUE),
                    city = sample(c("ATL", "NYC"), size = 200, replace = TRUE),
                    charges = sample(1000:2000, size = 200),
                    payment = sample(1000:2000, size = 200))

head(sm)

summary_stats(sm,
              condition    = city == "ATL",
              group_vars   = provider,
              summary_vars = c(charges, payment),
              arr          = provider)
}