lubridate (version 1.2.0)

parse_date_time: Function to parse character and numeric date-time vectors with user friendly order formats.

Description

As compared to strptime parser, parse_date_time allows to specify only the order in which the formats occur instead of the full format. As it was specifically designed to handle heterogeneous date-time formats at once, you can specify several alternative orders. parse_date_time sorts the supplied formats based on a training set and then applies them recursively on the input vector.

Usage

parse_date_time(x, orders, tz = "UTC", truncated = 0,
    quiet = FALSE, locale = Sys.getlocale("LC_TIME"),
    select_formats = .select_formats)

Arguments

x
a character or numeric vector of dates
orders
a character vector of date-time formats. Each order string is series of formatting characters as listed strptime but might not include the "%" prefix, for example "ymd" will match all the possi
tz
a character string that specifies the time zone with which to parse the dates
truncated
integer, number of formats that can be missing. The most common type of irregularity in date-time data is the truncation due to rounding or unavailability of the time stamp. If truncated parameter is non-zero parse_date_time
quiet
logical. When TRUE function evaluates without displaying customary messages.
locale
locale to be used, see locales. On linux systems you can use system("locale -a") to list all the installed locales.
select_formats
A function to select actual formats for parsing from a set of formats which matched a training subset of x. This should be function receiving a named integer vector and returning a character vector of selected formats. Names of the in

Value

  • a vector of POSIXct date-time objects

Details

Below are all the implemented formats recognized by lubridate. For all numeric formats leading 0s are optional. All formats are case insensitive. As compared to strptime, some of the formats have been extended for efficiency reasons. They are marked with "*"

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

See Also

strptime, ymd, ymd_hms

Examples

Run this code
x <- c("09-01-01", "09-01-02", "09-01-03")
parse_date_time(x, "ymd")
parse_date_time(x, "%y%m%d")
parse_date_time(x, "%y %m %d")
#  "2009-01-01 UTC" "2009-01-02 UTC" "2009-01-03 UTC"

## ** heterogenuous formats **
x <- c("09-01-01", "090102", "09-01 03", "09-01-03 12:02")
parse_date_time(x, c("%y%m%d", "%y%m%d %H%M"))

## different ymd orders:
x <- c("2009-01-01", "02022010", "02-02-2010")
parse_date_time(x, c("%d%m%Y", "ymd"))
##  "2009-01-01 UTC" "2010-02-02 UTC" "2010-02-02 UTC"

## ** truncated time-dates **
x <- c("2011-12-31 12:59:59", "2010-01-01 12:11", "2010-01-01 12", "2010-01-01")
parse_date_time(x, "%Y%m%d %H%M%S", truncated = 3)
parse_date_time(x, "ymd_hms", truncated = 3)
## "2011-12-31 12:59:59 UTC" "2010-01-01 12:11:00 UTC" "2010-01-01 12:00:00 UTC" "2010-01-01 00:00:00 UTC"

Run the code above in your browser using DataCamp Workspace