Skip to contents

Filters an econanalyzr-valid data frame by a closed date interval [start_date, end_date]. If start_date is not supplied, you can specify a period (period_type + period_amount) and the function will compute start_date = end_date - period (with end_date defaulting to the latest non-NA date in df).

Usage

econ_filter_dates(
  df,
  start_date = NULL,
  end_date = NULL,
  period_type = c("days", "weeks", "months", "quarters", "years"),
  period_amount = NULL,
  datetime_tz = "UTC",
  quiet = FALSE
)

Arguments

df

A data frame validated by check_econanalyzr_df().

start_date

Optional Date or POSIXt. If NULL, you must provide period_type and period_amount to derive it.

end_date

Optional Date or POSIXt. If NULL, defaults to the latest non-NA date in df.

period_type

Optional character scalar: one of "days", "weeks", "months", "quarters", "years". Used only if start_date is NULL.

period_amount

Optional positive integer-like scalar (e.g., 6 for "last 6 months"). Used only if start_date is NULL.

datetime_tz

Timezone for coercing POSIXt inputs to Date. Default "UTC".

quiet

If FALSE (default), emit a short message describing the filter.

Value

The filtered data frame, arranged by descending date.

Details

The interval is inclusive: date >= start_date and date <= end_date.

Examples

# Last 6 months ending at the table’s latest date
# econ_filter_dates(df, period_type = "months", period_amount = 6)

# Between two explicit dates
# econ_filter_dates(
#   df,
#   start_date = as.Date("2024-01-01"),
#   end_date   = as.Date("2024-12-01")
# )