2

I have a csv dataset that has date of births stored as characters in the format - 01-06-68("%d-%m-%y"). I tried to convert the dates to Date objects using as.Date() but it identifies the year as 2068 when in fact the year is 1968. I understand Date starts from 1970-01-01 in R, is there an easy fix to this?

I am using the lubridate package to find the age. Some age were negative due to the wrongly identified year.

elapsed <- train$Date.of.Birth %--% Sys.Date()
train$age <- floor(as.duration(elapsed) / dyears(1))
Alan
  • 21
  • 3

1 Answers1

0

Maybe try

format(as.Date(date,format="%d-%m-%y"), "19%y-%m-%d")

You probably need to use %Y anyway bite the bullet and create a new df with complete years else your are screwed when 19xx transitions to 20xx.

Good Luck.

Bruno
  • 3,236
  • 1
  • 6
  • 25
  • Thank you, but you were close, the answer for (https://stackoverflow.com/questions/9508747/add-correct-century-to-dates-with-year-provided-as-year-without-century-y) had the following solution : `d "2012-12-31", format(d, "19%y-%m-%d"), format(d)))` – Alan Apr 14 '19 at 07:32