Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date import, incorrect century

Tags:

date

r

I have a bunch of dates that I am parsing that are in the form "%m/%d/%y". as.Date(dates, format = "%m/%d/%y") converts a date like "1/01/64" to "2064-01-01" but I need that to be "1964-01-01." I suppose I can find instances where the year is in the future and then subtract a century, but that seems a little ridiculous.

like image 842
Zach Avatar asked Feb 18 '26 05:02

Zach


2 Answers

Dates are stored internal as integer days, so there is only such formatting at the time of input or output. As for input without century information I think you are out of luck. Here's what ?strptime says about the %y format spec: "On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say ‘it is expected that in a future version the default century inferred from a 2-digit year will change’."

  as.Date( "01/01/64", "%m/%d/%y", origin="1970-01-01") -100*365.25
  #[1] "1964-01-01"

It might be possible to start a bar fight about programmers who allow removal of century information given that Y2K is so recent in the past.

Since the default is to assume year 00-68 is 2000-2068, it is certainly possible to create an as.Dateshift

like image 116
IRTFM Avatar answered Feb 20 '26 21:02

IRTFM


Another way to fix the dates is to change all years that occur in the future (relative to today's date using Sys.Date()) as starting with 19 instead of 20.

dates=as.Date(c("01/01/64", "12/31/15"))
# [1] "2064-01-01" "2015-12-31" ## contains an incorrect date

## Now correct the dates that havn't yet occurred
as.Date(ifelse(dates > Sys.Date(), format(dates, "19%y-%m-%d"), format(dates)))
#[1] "1964-01-01" "2015-12-31"
like image 40
Justina Pinch Avatar answered Feb 20 '26 21:02

Justina Pinch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!