This takes a data set with a date variable and calculates year-on-year changes for a set of variables of your choice. Returns a data.table.

scale_yoy(
  data,
  yoy_vars,
  date_var = "date",
  leap_year_fillin = TRUE,
  by = NULL,
  growth = TRUE,
  format_percent = FALSE,
  accuracy = 0.1
)

Arguments

data

Any type of data set that can be coerced to a data.table.

yoy_vars

String vector of the variable names you want to calculate year-on-year change for.

date_var

The name of the date variable, as a string. Must be formatted as Date objects.

leap_year_fillin

If the date is Feb. 29, the previous year will not have a Feb. 29. Set to TRUE to fill in by linear interpolation. If set to TRUE, returned data will be sorted by by and date_var.

by

Character vector of the variable names you'd like the operation to be performed by. There should only be one observation per date per combination of by.

growth

Set to TRUE to get new/old - 1 (i.e. a percentage growth). Set to FALSE to get new/old (i.e. a relative value).

format_percent

Set to TRUE to get back a formatted percentage, i.e. "50%", instead of a number.

accuracy

If format_percent = TRUE, the number of digits after the decimal place to round to, as in scales::percent.

Details

This will add new variables using yoy_vars, adding lag and YOY variants.

Examples


# Create some fake data to do year-on-year calculations with
patterns <- data.table::data.table(date = c(lubridate::ymd('2019-01-15'),
                                lubridate::ymd('2019-01-16'),
                                lubridate::ymd('2020-01-15'),
                                lubridate::ymd('2020-01-16')),
                                visits_by_day = c(1,2,3,4))

# And scale relative to the year before!
scale_yoy(patterns, 'visits_by_day')[]
#>          date visits_by_day visits_by_day_lag visits_by_day_YOY
#> 1: 2019-01-15             1                NA                NA
#> 2: 2019-01-16             2                NA                NA
#> 3: 2020-01-15             3                 1                 2
#> 4: 2020-01-16             4                 2                 1