3

I have a date column in a data frame in chr format as follows:

chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"

I want to convert to date using something like:

strptime(x=data$dob, '%d-%b-%y')

But I get several future dates in the result like

[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"

Is there a way to ensure I return dates that commenced in the correct century?

Thanks

John
  • 41,131
  • 31
  • 82
  • 106

3 Answers3

4

It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.

Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).

# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65", 
        "19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')

# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Thanks @Josh, I used your ifelse with the year function in lubridate to do this. `data$year <- ifelse(year(data$dob) > year(Sys.time()), year(data$dob)-100 , year(data$dob))` then pasted together and reparsed with strptime. – John Apr 04 '12 at 04:52
2
    dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65", 
        "19-Nov-56", "28-Nov-59", "19-Apr-86")
    library(lubridate)
    DD=dmy(dd)

https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
0
strptime(data$dob, "%Y/%m/%d")