Convert input in any one of character, integer, numeric, factor, or ordered type into 'POSIXct' (or 'Date') objects, using one of a number of predefined formats, and relying on Boost facilities for date and time parsing.
R excels at computing with dates, and times. Using typed representation for your data is highly recommended not only because of the functionality offered but also because of the added safety stemming from proper representation.
But there is a small nuisance cost in interactive work as well as in
programming. How often have we told
as.POSIXct() that the
origin is (of course) the
epoch. Do we really
have to say it again? Similarly, when parsing dates that are
somewhat in YYYYMMDD format, do we really need to bother
converting from integer or numeric or character or factor or ordered
with one of dozen separators and/or month forms: YYYY-MM-DD, YYYY/MM/DD, YYYYMMDD,
YYYY-mon-DD and so on?
So there may have been a need for a general purpose converter
returning a proper
Date) object nomatter the
input (provided it was somewhat parseable).
anytime() tries to
be that function.
Conversion is done by looping over a fixed set of formats until a
matching one is found, or returning an error if none is found. The
current set of conversion formulae is accessible in the
and can now also be accessed in R via
getFormats(). Formats can
be added via the
Details on the Boost date format symbols are provided by the
date_time documentation and similar (but identical) to what
Boost date_time: http://www.boost.org/doc/libs/1_61_0/doc/html/date_time.html
Boost format documentation: http://www.boost.org/doc/libs/1_61_0/doc/html/date_time/date_time_io.html#date_time.format_flags
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
library(anytime) options(digits.secs=6) ## for fractional seconds below Sys.setenv(TZ=anytime:::getTZ()) ## helper function to try to get TZ ## integer anytime(20160101L + 0:2) ## numeric anytime(20160101 + 0:2) ## factor anytime(as.factor(20160101 + 0:2)) ## ordered anytime(as.ordered(20160101 + 0:2)) ## Dates: Character anytime(as.character(20160101 + 0:2)) ## Dates: alternate formats anytime(c("20160101", "2016/01/02", "2016-01-03")) ## Datetime: ISO with/without fractional seconds anytime(c("2016-01-01 10:11:12", "2016-01-01 10:11:12.345678")) ## Datetime: ISO alternate (?) with 'T' separator anytime(c("20160101T101112", "20160101T101112.345678")) ## Short month '%b' (and full month is supported too) anytime(c("2016-Sep-01 10:11:12", "Sep/01/2016 10:11:12", "Sep-01-2016 10:11:12")) ## Datetime: Mixed format (cf http://stackoverflow.com/questions/39259184) anytime(c("Thu Sep 01 10:11:12 2016", "Thu Sep 01 10:11:12.345678 2016"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.