Date format in R

I have a date column in a dataframe in chr format like this:

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 am getting multiple future dates as a 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 that I return dates that started in the correct century?

thank

+3


source to share


3 answers


It doesn't look (from the documentation for %y

at ?strptime

) as there is an obvious option for changing the default century inferred from two-digit years.

Since the objects returned strptime()

are of the POSIXlt class , but it's a pretty simple question 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"

      

+4


source


    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

+2


source


strptime(data$dob, "%Y/%m/%d")

      

0


source







All Articles