scale_to_date.Rd
Pick a date and provide some variables. Those variables will be adjusted to be relative to their value on that date. Usually used to calculate foot traffic growth relative to a certain date.
scale_to_date(
data,
adj_vars,
date,
date_var = "date",
by = NULL,
growth = TRUE,
format_percent = FALSE,
accuracy = 0.1
)
Any type of data set that can be coerced to a data.table
. Note that a data.table
will be returned.
Character vector of the variable names you'd like adjusted to be relative to the date.
The date you'd like everything relative to, as a date object.
The name of the date variable, as a string.
Character vector of the variable names you'd like the operation to be performed by. There should only be one observation for which date_var == date
within each combination of the by
variables, or else your results will be arbitrary.
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).
Set to TRUE
to get back a formatted percentage, i.e. "50%", instead of a number.
If format_percent = TRUE
, the number of digits after the decimal place to round to, as in scales::percent
.
# Create some data to scale relative to
patterns <- data.table(date = c(lubridate::ymd('2020-01-15'),
lubridate::ymd('2020-01-16'),
lubridate::ymd('2020-01-17')),
visits_by_day = c(1,2,3))
#> Error in data.table(date = c(lubridate::ymd("2020-01-15"), lubridate::ymd("2020-01-16"), lubridate::ymd("2020-01-17")), visits_by_day = c(1, 2, 3)): could not find function "data.table"
# Make everything relative to January 15!
scale_to_date(patterns, 'visits_by_day', lubridate::ymd('2020-01-15'))[]
#> Error in data.table::is.data.table(data): object 'patterns' not found