R as.Date conversion century error

后端 未结 1 1519
逝去的感伤
逝去的感伤 2021-01-25 05:21

In my dataset a column contains Date of Births of many employees so many of them lies in the range 1960 to 1980. I am trying to format them using as.Date and in some of them the

相关标签:
1条回答
  • 2021-01-25 05:49

    Read:

    ?strptime  # where all the formatting details are available
    

    %y
    Year without century (00–99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behavior 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’.

    So you need a regex to backdate and it's probably better to do as a string conversion before sending to as.Date:

    dvec <- c("7/1/61", "7/1/79")
    as.Date(  sub("/(..$)", "/19\\1",dvec)  , "%m/%d/%Y")
     [1] "1961-07-01" "1979-07-01"
    

    If this goes into production it will become an error waiting to happen when the age of your employees starts to creep above the last two digits of the current year.

    0 讨论(0)
提交回复
热议问题