scale_to_date: Scale data relative to its value on a date

View source: R/scale_to_date.R

scale_to_dateR Documentation

Scale data relative to its value on a date

Description

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.

Usage

scale_to_date(
  data,
  adj_vars,
  date,
  date_var = "date",
  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. Note that a data.table will be returned.

adj_vars

Character vector of the variable names you'd like adjusted to be relative to the date.

date

The date you'd like everything relative to, as a date object.

date_var

The name of the date variable, as a string.

by

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.

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.

Examples

# 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))

# Make everything relative to January 15!
scale_to_date(patterns, 'visits_by_day', lubridate::ymd('2020-01-15'))[]

SafeGraphInc/SafeGraphR documentation built on Nov. 25, 2022, 11:20 a.m.