Converting dates before January 1, 1970 in R

前端 未结 3 1588
予麋鹿
予麋鹿 2021-01-27 06:05

I am trying to convert a column of dates into Date objects in R, but I can\'t seem to get the desired results. These individuals have birth dates before January 1, 1970, so when

3条回答
  •  被撕碎了的回忆
    2021-01-27 06:27

    No need for add-on packages, base R is fine. But you need to specify the century:

    R> as.Date("1954-01-12")
    [1] "1954-01-12"
    R> 
    

    If you need non-default formats, just specify them:

    R> as.Date("19540112", "%Y%m%d")
    [1] "1954-01-12"
    R> 
    

    Edit: In case your data really comes in using the %y% format, and you happen to make the policy decision that the 19th century is needed , here is one base R way of doing it:

    R> d <- as.Date("540112", "%y%m%d")
    R> dlt <- as.POSIXlt(d)
    R> dlt$year <- dlt$year - 100
    R> as.Date(dlt)
    [1] "1954-01-12"
    R> 
    

提交回复
热议问题