Skip to contents

Filter a data.frame by specific date range.

Usage

filter_date(x, colname = NULL, ref = Sys.Date(), unit = "month")

Arguments

x

a data.frame with a Date or POSIXct column.

colname

an optional character string, the name of the column to use in filter.

ref

an optional Date value to be used as a reference for the date range.

unit

a string to compute the date range (default = "month") (see round_date).

Value

a filtered data.frame.

Details

The purpose of this function is to make data navigation between date range easier. By default, it will filter the input data.frame to keep dates belonging to the current month.

When colname = NULL (the default), it will look for a 'date' column in x.

Examples

# -- this month (the default)
filter_date(data.frame(date = Sys.Date() - runif(n = 10, min = -50, max = 50)))
#>         date
#> 1 2026-10-05
#> 2 2026-10-02
#> 3 2026-10-23

# -- previous month
filter_date(data.frame(date = Sys.Date() - runif(n = 10, min = -50, max = 50)),
ref = Sys.Date() - as.integer(format(Sys.Date(), "%d")))
#> [1] date
#> <0 rows> (or 0-length row.names)