0

I am working with date data. Below is sample data:

id    dob  
1     8/2/48
2     12/13/34

I am using the code below to transform the date data from %m/%d/%y to y-m-d.

library(read_r)
df$new_dob <- parse_datetime(df$date_birth, "%m/%d/%y")

Using this code, I get the data in the right format, but the year is off.

id    dob        new_dob
1     8/2/48     2048-08-02
2     12/13/34   2034-12-13

It should be this:

id    dob        new_dob
1     8/2/48     1948-08-02
2     12/13/34   1934-12-13

Does anyone know how I can get this to work?

D. Fowler
  • 443
  • 2
  • 5

1 Answers1

0

When you have year without century all the years from 00 to 68 are prefixed with 20 and 69 to 99 by 19. For this case you can subtract 100 years after converting the data.

library(lubridate)
as.Date(parse_date_time(df$dob, c('%d/%m/%y', '%m/%d/%y'))) - years(100)
#[1] "1948-08-02" "1934-12-13"
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143