Filter a data frame by a column to rows with the largest value of that column that is less than or equal to specified maximum value.
Source:R/utils.R
filter_largest_lte.RdUseful for getting the last date prior to or on a given target date (e.g. matching data vintages).
Arguments
- df
data frame to filter
- column
column to filter on.
- max_value
Maximum value. Filter to the largest value in
columnless than or equal tomax_value.- .by
Optional grouping columns in
dffor the filter. Passed as the.byargument todplyr::filter(). DefaultNULL, matching thedplyr::filter()default.- .preserve
Preserve all groups present in grouped input? Passed as the
.preverseargument todplyr::filter(). DefaultFALSE, matching thedplyr::filter()default.
Details
Uses dplyr::filter() syntax, and accepts data-masked
expressions for column and max_value.
Returns a 0-row tibble if no rows match the criteria.
Examples
some_dates <- tibble::tibble(
row_no = 1:3,
date = as.Date(c("2026-01-01", "2026-07-02", "2026-07-03"))
)
some_dates |> filter_largest_lte(date, as.Date("2026-07-02"))
#> # A tibble: 1 × 2
#> row_no date
#> <int> <date>
#> 1 2 2026-07-02
some_dates |> filter_largest_lte(date, as.Date("2026-07-03"))
#> # A tibble: 1 × 2
#> row_no date
#> <int> <date>
#> 1 3 2026-07-03
some_dates |> filter_largest_lte(date, as.Date("2026-07-01"))
#> # A tibble: 1 × 2
#> row_no date
#> <int> <date>
#> 1 1 2026-01-01