dateToPeriod: Convert dates to a relative period

View source: R/dateToPeriod.R

dateToPeriodR Documentation

Convert dates to a relative period

Description

Create a relative period variable from a date variable

Usage

dateToPeriod(
  trans_df,
  date,
  periodicity = NULL,
  min_date = NULL,
  max_date = NULL,
  adj_type = "move",
  ...
)

Arguments

trans_df

data.frame of raw transactions

date

name of field containing the date of the sale in Date or POSIXt format

periodicity

type of periodicity to use ('yearly', 'quarterly', 'monthly' or 'weekly)

min_date

default = NULL; optional minimum date to use

max_date

default = NULL; optional maximum date to use

adj_type

default = 'move'; how to handle min and max dates within the range of transactions. 'move' min and/or max date or 'clip' the data

...

Additional arguments

Value

original data frame ('trans_df' object) with two new fields: trans_period: integer value counting from the minimum transaction date in the periodicity selected. Base value is 1. Primarily for modeling trans_date: properly formatted transaction date

Further Details

"trans_period" counts from the minimum transaction date provided. As such the period counts are relative, not absolute

Additionally, this function modifies the data.frame that it is given and return that same data.frame that it is given and returns that data.frame with the new fields attached.

Examples


# Load data
  data(ex_sales)

# Convert to period df
  hpi_data <- dateToPeriod(trans_df = ex_sales,
                           date = 'sale_date',
                           periodicity = 'monthly')


andykrause/hpiR documentation built on Feb. 2, 2024, 8:13 p.m.