Keep only rows whose period is complete as of a cutoff
Source:R/period_completeness.R
filter_complete_periods.RdFilters data to the rows whose period (per period_is_complete()) is
complete as of as_of. Useful for dropping the in-progress trailing period
from a summary or year-over-year comparison.
Usage
filter_complete_periods(
data,
date_col,
unit = "month",
as_of = NULL,
calendar = "WeekendsOnly",
week_start = getOption("lubridate.week.start", 7)
)Arguments
- data
A data frame.
- date_col
<
data-masked> The date column whose period completeness is tested.- unit
Period size, one of
"week","month","quarter", or"year".- as_of
Reference cutoff date, a single Date (or coercible). Defaults to
NULL, in which case it is set to the maximum ofdate_colso completeness is judged against how current the data actually is. An explicitas_ofearlier than the maximum ofdate_colis an error, because it would drop periods the data demonstrably completes; useperiod_is_complete()directly for point-in-time completeness.- calendar
(character) A QuantLib calendar id (see qlcal::calendars). Defaults to
"WeekendsOnly".- week_start
Integer 1-7 giving the first day of the week (only used when
unit = "week"). Defaults togetOption("lubridate.week.start", 7).
Examples
df <- data.frame(day = seq(as.Date("2025-04-01"), as.Date("2025-06-12"), by = "day"))
# as_of defaults to max(day) = 2025-06-12, so the partial June is dropped
nrow(filter_complete_periods(df, day, unit = "month"))
#> [1] 61