Skip to contents

Useful for getting the last date prior to or on a given target date (e.g. matching data vintages).

Usage

filter_largest_lte(df, column, max_value, .by = NULL, .preserve = FALSE)

Arguments

df

data frame to filter

column

column to filter on.

max_value

Maximum value. Filter to the largest value in column less than or equal to max_value.

.by

Optional grouping columns in df for the filter. Passed as the .by argument to dplyr::filter(). Default NULL, matching the dplyr::filter() default.

.preserve

Preserve all groups present in grouped input? Passed as the .preverse argument to dplyr::filter(). Default FALSE, matching the dplyr::filter() default.

Value

The filtered data frame.

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