View source: R/scale_to_date.R
scale_yoy | R Documentation |
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 )
data |
Any type of data set that can be coerced to a |
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 |
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 |
growth |
Set to |
format_percent |
Set to |
accuracy |
If |
This will add new variables using yoy_vars
, adding lag
and YOY
variants.
# 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')[]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.