Filter Data Frame by Date Range
filter_by_date.Rd
This function filters a data frame to include only rows where a specified date column
falls within a given start and/or end date. The function accepts Date
objects
or character strings (formatted as "yyyy-mm-dd"
) for date_start
and
date_end
parameters.
Arguments
- data
A data frame containing the date column to filter by.
- date_var
A character string specifying the name of the date column in
data
. Default is"date_report"
.- date_start
A
Date
object or character string in"yyyy-mm-dd"
format, orNULL
. If provided, only rows wheredate_var
is greater than or equal todate_start
are included. Default isNULL
.- date_end
A
Date
object or character string in"yyyy-mm-dd"
format, orNULL
. If provided, only rows wheredate_var
is less than or equal todate_end
are included. Default isNULL
.
Examples
# Example data frame
if (FALSE) { # \dontrun{
data <- data.frame(
date_report = as.Date("2023-01-01") + 0:9,
value = rnorm(10)
)
# Filter data from January 3, 2023 to January 8, 2023 (using Date format)
filtered_data <- filter_by_date(data,
date_var = "date_report",
date_start = as.Date("2023-01-03"),
date_end = as.Date("2023-01-08")
)
# Filter data using character format for dates
filtered_data <- filter_by_date(data,
date_var = "date_report",
date_start = "2023-01-03",
date_end = "2023-01-08"
)
} # }