
Apply a function to an econanalyzr data frame's numeric column after date filtering
Source:R/econ_value_summary.R
econ_value_summary.RdFilters 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(orPOSIXt) scalar/vector; rows withdate %in% datesare included/excluded perfilter_type.POSIXtis coerced toDateusingdates_tz.- date_range
Optional length-2
Date/POSIXtgiving a closed range[min(date_range), max(date_range)]. Ignored ifdatesis provided.- val_col
Column to operate on (must be numeric): tidy-select name. Defaults to column name
valuethat 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, orfunction(x) stats::quantile(x, c(.25,.5,.75)).- na_rm
Logical; if
TRUE(default) removeNAs before calling.fun.- dates_tz
Timezone for coercing
POSIXt(dates or range) toDate. Default"UTC".- empty_ok
If
FALSE(default) and the filter returns 0 rows, returnNA_real_with a warning. IfTRUE, pass a length-0 vector to.fun(which may error).- ...
Extra arguments forwarded to
.fun.
Details
Rules:
If both
datesanddate_rangeareNULL, the whole data is used.If both are supplied, a classed error is raised.
If the filter yields 0 rows and
empty_ok = FALSE, returnsNA_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))
# )