Skip to contents

Filters an econanalyzr-valid data frame by either a set of membership dates (dates) or a closed range (date_range), pulls a numeric column, and applies a user-supplied function .fun to that vector.

Usage

econ_value_summary(
  df,
  dates = NULL,
  date_range = NULL,
  val_col = "value",
  filter_type = c("inclusive", "exclusive"),
  .fun = mean,
  na_rm = TRUE,
  dates_tz = "UTC",
  empty_ok = FALSE,
  ...
)

Arguments

df

A data frame validated by check_econanalyzr_df().

dates

Optional Date (or POSIXt) scalar/vector; rows with date %in% dates are included/excluded per filter_type. POSIXt is coerced to Date using dates_tz.

date_range

Optional length-2 Date/POSIXt giving a closed range [min(date_range), max(date_range)]. Ignored if dates is provided.

val_col

Column to operate on (must be numeric): tidy-select name. Defaults to column name value that is standard with econanalyzr data frames.

filter_type

"inclusive" (keep matches) or "exclusive" (drop matches).

.fun

A function (or function name) applied to the pulled vector, e.g. mean, median, or function(x) stats::quantile(x, c(.25,.5,.75)).

na_rm

Logical; if TRUE (default) remove NAs before calling .fun.

dates_tz

Timezone for coercing POSIXt (dates or range) to Date. Default "UTC".

empty_ok

If FALSE (default) and the filter returns 0 rows, return NA_real_ with a warning. If TRUE, pass a length-0 vector to .fun (which may error).

...

Extra arguments forwarded to .fun.

Value

Whatever .fun returns (often a scalar; may be a vector).

Details

Rules:

  • If both dates and date_range are NULL, the whole data is used.

  • If both are supplied, a classed error is raised.

  • If the filter yields 0 rows and empty_ok = FALSE, returns NA_real_ and warns.

  • If na_rm = TRUE, NAs are removed before calling .fun.

Examples

# Mean of 'value' for a set of dates
# econ_value_summary(
#   df, dates = as.Date(c("2025-01-01","2025-02-01")), .fun = mean
# )

# Median over a date range (exclusive: drop rows in range)
# econ_value_summary(
#   df,
#   date_range = as.Date(c("2025-01-01","2025-03-31")),
#   filter_type = "exclusive",
#   .fun = median
# )

# Vector result (quantiles)
# econ_value_summary(
#   df,
#   dates = unique(df$date),
#   .fun  = function(x) stats::quantile(x, c(.25,.5,.75))
# )