armipunk armipunk - 1 year ago 74
R Question

Lubridate example ymd(090101, 90102)

I’m fairly new to R, and programming in general, and have a question. The answer is probably staring me in the face, but I’m not seeing it.

In package lubridate, one of the examples for


ymd(090101, 90102)

Which gets parsed as "
" "

My question - what if the missing sixth digit in '
' was related to the day, not the year? That is, the intended date was
? I can see at least one way this could happen: someone (me, a year ago trying to format dates for Solr, which had me using spreadsheets to separate dates into year, month and day columns and adding Ts and Zs all over) - and leading zeroes get truncated at some point. (Numbers on OS X does auto formatting a lot)

Separately, maybe something similar would happen in data.table::fread with a column of 90s dates of the form
, followed by a series of aughts dates from 2000 to 2009 and it autodetects integer first. Not sure. If
autodetects integer then that would be another way.

This is not a problem for me - was reading the documentation trying to solve another date problem and this example had me wondering why.

Answer Source

The answer is probably staring me in the face

With dates? No, if you're confused with date handling, you're probably paying attention. Date handling is a surprisingly messy business (even without involving computers), and the software for date-handling simply reflects this.

As to your question: parse_date_time() and its orders parameter are the easiest way to handle this in R. The scenario you describe is actually catered for. Check out the help page, and scroll down to the weird symbols I've put in the "orders" parameter.

datestr <- c("090101", "90102")
parsed <- parse_date_time(datestr, orders=c("ymd"))
parsed_date <- as.Date(parsed)
[1] "2009-01-01" "1990-10-02" 

If, on the other hand, your issue is that sometimes 90102 means '2009-01-02' and sometimes it means '1990-01-02', but it depends on which method you used to massage the data: you'll have to tell the software which case is which, just like you would a human being. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download