Skip to contents

[Deprecated]

Converts a single improperly formatted date to R's Date class. Supports numerous separators including /,- or white space. Supports all-numeric, abbreviation or long-hand month notation. Where day of the month has not been supplied, the first day of the month is imputed. Either DMY or YMD is assumed by default. However, the US system of MDY is supported via the format argument.

Usage

fix_date(date, day.impute = 1, month.impute = 7, format = "dmy")

Arguments

date

Character to be converted to R's Date class.

day.impute

Integer between 1 and 31, or NA, or NULL. Day of the month to be imputed when missing. Defaults to 1. If day.impute = NA, then NA will be imputed for the date and a warning will be raised. If day.impute = NULL, the function will fail with an error when day is missing.

month.impute

Integer between 1 and 12, or NA, or NULL. Month to be imputed when missing. Defaults to 7 (July). If month.impute = NA, then NA will be imputed for the entire date and a warning will be raised. If month.impute = NULL, the function will fail with an error when month is missing.

format

Character string specifying date interpretation preference. Either "dmy" (day-month-year, default) or "mdy" (month-day-year, US format). This setting only affects ambiguous numeric dates like "01/02/2023". When month names are present or year appears first, the format is auto-detected regardless of this parameter. Note that unambiguous dates (e.g., "25/12/2023") are parsed correctly regardless of the format setting.

Value

An object belonging to R's built-in Date class.

See also

fix_date_char for character vectors and fix_date_df for data frames.

Examples

bad.date <- "02 03 2021"
fixed.date <- fix_date(bad.date)
#> Warning: `fix_date()` was deprecated in datefixR 1.0.0.
#>  Please use `fix_date_char()` instead.
fixed.date
#> [1] "2021-03-02"
# - Due to deprecation, prefer using fix_date_char.
fixed.date <- fix_date_char(bad.date)