| coerce_dates | R Documentation |
For each specified column, attempts to coerce the values to Date.
Values that cannot be parsed (e.g. "not recorded" appearing in an
otherwise date-valued column) are replaced with NA. A message lists
the dropped values per column so the user is aware of what was discarded.
Use suppressMessages() to silence.
coerce_dates(
data,
...,
formats = c("%Y-%m-%d", "%Y/%m/%d", "%d-%m-%Y", "%d/%m/%Y")
)
data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). |
... |
< |
formats |
character vector of date formats to try, in order, as
accepted by |
Columns which are already Date objects are left untouched. Date-time
(POSIXct) columns are converted to Date, discarding the time of
day; a message reports this, as it is a loss of information.
Dates are parsed with the formats given in formats, tried in order.
The default covers the ISO 8601 form ("2020-01-15") and the common
unambiguous variants. Note that formats are not guessed per value: a column
is parsed with the first format which succeeds for all of its non-missing
values, so mixed-format columns will report the values that did not parse.
data with the specified columns coerced to Date.
coerce_numeric_vars
df <- data.frame(x = c("2020-01-15", "2020-02-30", "not recorded"),
y = c("a", "b", "c"))
# "2020-02-30" is not a real date and "not recorded" is not a date at all,
# so both become NA and are reported:
coerce_dates(df, x)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.