fix_date_df: Clean up messy date columns

View source: R/fix_date_df.R

fix_date_dfR Documentation

Clean up messy date columns

Description

Tidies a dataframe object which has date columns entered via a free-text box (possibly by different users) and are therefore in a non-standardized format. Supports numerous separators including /,-, or 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_df(
  df,
  col.names,
  day.impute = 1,
  month.impute = 7,
  id = NULL,
  format = "dmy"
)

Arguments

df

A dataframe or tibble object with messy date column(s)

col.names

Character vector of names of columns of messy date data

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.

id

Name of column containing row IDs. By default, the first column is assumed.

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 dataframe or tibble object. Dependent on the type of df. Selected columns are of type Date with the following format yyyy-mm-dd

See Also

fix_date_char which is similar to fix_date_df() except can only be applied to character vectors.

Examples

data(exampledates)
fixed.df <- fix_date_df(exampledates, c("some.dates", "some.more.dates"))
fixed.df

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