lubridate (version 0.1)

guess_format: Guess the format of dates in a character or numeric vector...

Description

Guess the format of dates in a character or numeric vector

Usage

guess_format(x, formats, seps=c("-", "/", ""))

Arguments

x
a character or numeric vector of suspected dates
formats
a list of formats to test. Each format should be a vector of date-time format elements. To test an alternative order of elements, the alternative order should be entered as an additional format. See link[base]{strptime} for format elements.
seps
a vector of possible characters used to separate elements within the dates.

Value

  • a character string of the most likely date-time format

Details

Returns the format that successfully parses the most dates within a character or numeric vector to POSIXct objects. If multiple formats are equally successful, guess format will display the successful formats in a message and select the first format by default. guess_format assumes that each date only uses one type of separator and that all dates use the same separator.

See Also

find_separator

Examples

Run this code
x <- c("2009/01/01", "2009/01/02", "2009/01/03")
guess_format(x, list(c("%y", "%m", "%d"), c("%Y", "%m", "%d")), seps = c("-", "", "/"))
# "\%Y/\%m/\%d"
x <- c("20090101", "20090102", "20090103")
guess_format(x, list(c("%y", "%m", "%d"), c("%Y", "%m", "%d")), seps = c("-", "", "/"))
#  "\%Y\%m\%d" 
x <- c("09-01-01", "09-01-02", "09-01-03")
guess_format(x, list(c("%y", "%m", "%d"), c("%Y", "%m", "%d")), seps = c("-", "", "/"))
#  Multiple format matches with 3 successes: \%y-\%m-\%d, \%Y-\%m-\%d.
#         1 
# "\%y-\%m-\%d"}

Run the code above in your browser using DataCamp Workspace