fix_date_char: Convert improperly formatted date to R's Date class

View source: R/fix_date_char.R

fix_date_charR Documentation

Convert improperly formatted date to R's Date class

Description

Converts a character vector (or single character object) from improperly formatted dates 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 by default. Either DMY or YMD is assumed by default. However, the US system of MDY is supported via the format argument.

Usage

fix_date_char(dates, day.impute = 1, month.impute = 7, format = "dmy")

Arguments

dates

Character vector to be converted to R's date class.

day.impute

Integer. Day of the month to be imputed if not available. defaults to 1. If day.impute = NA then NA will be imputed for the date instead and a warning will be raised. If day.impute = NULL then instead of imputing the day of the month, the function will fail

month.impute

Integer. Month to be be imputed if not available. Defaults to 7 (July). If month.impute = NA then NA will be imputed for the date instead and a warning will be raised. If month.impute = NULL then instead of imputing the month, the function will fail.

format

Character. The format which a date is mostly likely to be given in. Either "dmy" (default) or "mdy". If year appears to have been given first, then YMD is assumed for the subject (format argument is not used for these observations)

Value

A vector of elements belonging to R's built in Date class with the following format yyyy-mm-dd.

See Also

fix_date_df which is similar to fix_date_char() except is applicable to columns of a data frame.

Examples

bad.date <- "02 03 2021"
fixed.date <- fix_date_char(bad.date)
fixed.date

datefixR documentation built on Sept. 22, 2022, 5:06 p.m.